@wishknish/knishio-client-ts 0.7.6 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,6 +50,10 @@ import Response from './Response'
50
50
  import Dot from '../libraries/Dot'
51
51
  import Molecule from '../core/Molecule'
52
52
  import type Query from '../query/Query'
53
+ import type BaseException from '../exception/BaseException'
54
+ import MolecularHashMismatchException from '../exception/MolecularHashMismatchException'
55
+ import SignatureMismatchException from '../exception/SignatureMismatchException'
56
+ import AtomIndexException from '../exception/AtomIndexException'
53
57
 
54
58
  /**
55
59
  * Response for proposing new Molecules
@@ -143,6 +147,49 @@ export default class ResponseProposeMolecule extends Response {
143
147
  return Dot.get(this.data(), 'reason', 'Invalid response from server') as string
144
148
  }
145
149
 
150
+ /**
151
+ * Map this rejection to a typed SDK exception when the failure mode is one
152
+ * we know about. Returns null on success or for rejections we don't have a
153
+ * typed class for.
154
+ *
155
+ * This is the single place pattern-matching against validator reason strings
156
+ * lives — consumers (KnishIOClient cache invalidation, callers needing to
157
+ * branch on failure mode) can `instanceof`-switch on the result instead.
158
+ * Future validator versions can rephrase reasons (or populate a structured
159
+ * field in the response payload) without breaking callers.
160
+ */
161
+ toException(): BaseException | null {
162
+ if (this.success()) return null
163
+ const reason = this.reason()
164
+ const lc = reason.toLowerCase()
165
+
166
+ if (/molecularhashmismatch|hash.*mismatch/.test(lc)) {
167
+ return new MolecularHashMismatchException(reason, {
168
+ details: { reason },
169
+ code: 'HASH_MISMATCH'
170
+ })
171
+ }
172
+ if (/ots.*position.*reuse|position.*already.*used|ots.*verification/.test(lc)) {
173
+ return new SignatureMismatchException(reason, {
174
+ details: { reason },
175
+ code: 'OTS_VERIFICATION_FAILED'
176
+ })
177
+ }
178
+ if (/continuid.*chain|previousposition|chain.*violation/.test(lc)) {
179
+ return new AtomIndexException(reason, {
180
+ details: { reason },
181
+ code: 'INDEX_CONFLICT'
182
+ })
183
+ }
184
+ if (/signature.*verification|signature.*invalid/.test(lc)) {
185
+ return new SignatureMismatchException(reason, {
186
+ details: { reason },
187
+ code: 'VERIFICATION_FAILED'
188
+ })
189
+ }
190
+ return null
191
+ }
192
+
146
193
  /**
147
194
  * Returns payload object
148
195
  * Matches JavaScript SDK payload method exactly