@pagopa/io-react-native-wallet 0.24.1 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. package/lib/commonjs/client/generated/wallet-provider.js +39 -16
  2. package/lib/commonjs/client/generated/wallet-provider.js.map +1 -1
  3. package/lib/commonjs/client/index.js +25 -10
  4. package/lib/commonjs/client/index.js.map +1 -1
  5. package/lib/commonjs/credential/issuance/03-start-user-authorization.js +1 -1
  6. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js +21 -14
  7. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js.map +1 -1
  8. package/lib/commonjs/credential/issuance/05-authorize-access.js +5 -2
  9. package/lib/commonjs/credential/issuance/05-authorize-access.js.map +1 -1
  10. package/lib/commonjs/credential/issuance/06-obtain-credential.js +26 -17
  11. package/lib/commonjs/credential/issuance/06-obtain-credential.js.map +1 -1
  12. package/lib/commonjs/credential/issuance/README.md +8 -14
  13. package/lib/commonjs/credential/issuance/errors.js +52 -0
  14. package/lib/commonjs/credential/issuance/errors.js.map +1 -0
  15. package/lib/commonjs/credential/issuance/index.js +7 -2
  16. package/lib/commonjs/credential/issuance/index.js.map +1 -1
  17. package/lib/commonjs/credential/issuance/types.js +1 -5
  18. package/lib/commonjs/credential/issuance/types.js.map +1 -1
  19. package/lib/commonjs/credential/presentation/01-start-flow.js +1 -1
  20. package/lib/commonjs/credential/presentation/01-start-flow.js.map +1 -1
  21. package/lib/commonjs/credential/presentation/03-get-request-object.js +2 -2
  22. package/lib/commonjs/credential/presentation/03-get-request-object.js.map +1 -1
  23. package/lib/commonjs/credential/presentation/04-send-authorization-response.js +2 -2
  24. package/lib/commonjs/credential/presentation/04-send-authorization-response.js.map +1 -1
  25. package/lib/commonjs/credential/presentation/errors.js +49 -0
  26. package/lib/commonjs/credential/presentation/errors.js.map +1 -0
  27. package/lib/commonjs/credential/presentation/index.js +5 -0
  28. package/lib/commonjs/credential/presentation/index.js.map +1 -1
  29. package/lib/commonjs/credential/status/02-status-attestation.js +8 -6
  30. package/lib/commonjs/credential/status/02-status-attestation.js.map +1 -1
  31. package/lib/commonjs/credential/status/README.md +5 -2
  32. package/lib/commonjs/credential/status/types.js +1 -14
  33. package/lib/commonjs/credential/status/types.js.map +1 -1
  34. package/lib/commonjs/sd-jwt/errors.js +40 -0
  35. package/lib/commonjs/sd-jwt/errors.js.map +1 -0
  36. package/lib/commonjs/sd-jwt/index.js +8 -4
  37. package/lib/commonjs/sd-jwt/index.js.map +1 -1
  38. package/lib/commonjs/sd-jwt/verifier.js +5 -1
  39. package/lib/commonjs/sd-jwt/verifier.js.map +1 -1
  40. package/lib/commonjs/trust/index.js +2 -2
  41. package/lib/commonjs/trust/index.js.map +1 -1
  42. package/lib/commonjs/utils/decoder.js +3 -1
  43. package/lib/commonjs/utils/decoder.js.map +1 -1
  44. package/lib/commonjs/utils/error-codes.js +51 -0
  45. package/lib/commonjs/utils/error-codes.js.map +1 -0
  46. package/lib/commonjs/utils/errors.js +119 -463
  47. package/lib/commonjs/utils/errors.js.map +1 -1
  48. package/lib/commonjs/utils/misc.js +21 -14
  49. package/lib/commonjs/utils/misc.js.map +1 -1
  50. package/lib/commonjs/utils/par.js +2 -1
  51. package/lib/commonjs/utils/par.js.map +1 -1
  52. package/lib/commonjs/wallet-instance/README.md +26 -5
  53. package/lib/commonjs/wallet-instance/index.js +33 -7
  54. package/lib/commonjs/wallet-instance/index.js.map +1 -1
  55. package/lib/commonjs/wallet-instance-attestation/README.md +8 -2
  56. package/lib/commonjs/wallet-instance-attestation/issuing.js +13 -10
  57. package/lib/commonjs/wallet-instance-attestation/issuing.js.map +1 -1
  58. package/lib/module/client/generated/wallet-provider.js +31 -11
  59. package/lib/module/client/generated/wallet-provider.js.map +1 -1
  60. package/lib/module/client/index.js +22 -8
  61. package/lib/module/client/index.js.map +1 -1
  62. package/lib/module/credential/issuance/03-start-user-authorization.js +1 -1
  63. package/lib/module/credential/issuance/04-complete-user-authorization.js +16 -9
  64. package/lib/module/credential/issuance/04-complete-user-authorization.js.map +1 -1
  65. package/lib/module/credential/issuance/05-authorize-access.js +7 -4
  66. package/lib/module/credential/issuance/05-authorize-access.js.map +1 -1
  67. package/lib/module/credential/issuance/06-obtain-credential.js +29 -20
  68. package/lib/module/credential/issuance/06-obtain-credential.js.map +1 -1
  69. package/lib/module/credential/issuance/README.md +8 -14
  70. package/lib/module/credential/issuance/errors.js +44 -0
  71. package/lib/module/credential/issuance/errors.js.map +1 -0
  72. package/lib/module/credential/issuance/index.js +3 -2
  73. package/lib/module/credential/issuance/index.js.map +1 -1
  74. package/lib/module/credential/issuance/types.js +0 -3
  75. package/lib/module/credential/issuance/types.js.map +1 -1
  76. package/lib/module/credential/presentation/01-start-flow.js +1 -1
  77. package/lib/module/credential/presentation/01-start-flow.js.map +1 -1
  78. package/lib/module/credential/presentation/03-get-request-object.js +3 -3
  79. package/lib/module/credential/presentation/03-get-request-object.js.map +1 -1
  80. package/lib/module/credential/presentation/04-send-authorization-response.js +3 -3
  81. package/lib/module/credential/presentation/04-send-authorization-response.js.map +1 -1
  82. package/lib/module/credential/presentation/errors.js +42 -0
  83. package/lib/module/credential/presentation/errors.js.map +1 -0
  84. package/lib/module/credential/presentation/index.js +2 -1
  85. package/lib/module/credential/presentation/index.js.map +1 -1
  86. package/lib/module/credential/status/02-status-attestation.js +11 -9
  87. package/lib/module/credential/status/02-status-attestation.js.map +1 -1
  88. package/lib/module/credential/status/README.md +5 -2
  89. package/lib/module/credential/status/types.js +0 -12
  90. package/lib/module/credential/status/types.js.map +1 -1
  91. package/lib/module/sd-jwt/errors.js +32 -0
  92. package/lib/module/sd-jwt/errors.js.map +1 -0
  93. package/lib/module/sd-jwt/index.js +5 -5
  94. package/lib/module/sd-jwt/index.js.map +1 -1
  95. package/lib/module/sd-jwt/verifier.js +5 -1
  96. package/lib/module/sd-jwt/verifier.js.map +1 -1
  97. package/lib/module/trust/index.js +3 -3
  98. package/lib/module/trust/index.js.map +1 -1
  99. package/lib/module/utils/decoder.js +3 -1
  100. package/lib/module/utils/decoder.js.map +1 -1
  101. package/lib/module/utils/error-codes.js +43 -0
  102. package/lib/module/utils/error-codes.js.map +1 -0
  103. package/lib/module/utils/errors.js +98 -438
  104. package/lib/module/utils/errors.js.map +1 -1
  105. package/lib/module/utils/misc.js +18 -11
  106. package/lib/module/utils/misc.js.map +1 -1
  107. package/lib/module/utils/par.js +3 -2
  108. package/lib/module/utils/par.js.map +1 -1
  109. package/lib/module/wallet-instance/README.md +26 -5
  110. package/lib/module/wallet-instance/index.js +32 -7
  111. package/lib/module/wallet-instance/index.js.map +1 -1
  112. package/lib/module/wallet-instance-attestation/README.md +8 -2
  113. package/lib/module/wallet-instance-attestation/issuing.js +15 -12
  114. package/lib/module/wallet-instance-attestation/issuing.js.map +1 -1
  115. package/lib/typescript/client/generated/wallet-provider.d.ts +138 -27
  116. package/lib/typescript/client/generated/wallet-provider.d.ts.map +1 -1
  117. package/lib/typescript/client/index.d.ts +7 -1
  118. package/lib/typescript/client/index.d.ts.map +1 -1
  119. package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts +1 -1
  120. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts +1 -1
  121. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts.map +1 -1
  122. package/lib/typescript/credential/issuance/05-authorize-access.d.ts.map +1 -1
  123. package/lib/typescript/credential/issuance/06-obtain-credential.d.ts.map +1 -1
  124. package/lib/typescript/credential/issuance/errors.d.ts +28 -0
  125. package/lib/typescript/credential/issuance/errors.d.ts.map +1 -0
  126. package/lib/typescript/credential/issuance/index.d.ts +3 -2
  127. package/lib/typescript/credential/issuance/index.d.ts.map +1 -1
  128. package/lib/typescript/credential/issuance/types.d.ts +0 -8
  129. package/lib/typescript/credential/issuance/types.d.ts.map +1 -1
  130. package/lib/typescript/credential/presentation/03-get-request-object.d.ts.map +1 -1
  131. package/lib/typescript/credential/presentation/04-send-authorization-response.d.ts.map +1 -1
  132. package/lib/typescript/credential/presentation/errors.d.ts +25 -0
  133. package/lib/typescript/credential/presentation/errors.d.ts.map +1 -0
  134. package/lib/typescript/credential/presentation/index.d.ts +2 -1
  135. package/lib/typescript/credential/presentation/index.d.ts.map +1 -1
  136. package/lib/typescript/credential/status/02-status-attestation.d.ts.map +1 -1
  137. package/lib/typescript/credential/status/types.d.ts +0 -15
  138. package/lib/typescript/credential/status/types.d.ts.map +1 -1
  139. package/lib/typescript/sd-jwt/errors.d.ts +20 -0
  140. package/lib/typescript/sd-jwt/errors.d.ts.map +1 -0
  141. package/lib/typescript/sd-jwt/index.d.ts +3 -2
  142. package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
  143. package/lib/typescript/utils/error-codes.d.ts +45 -0
  144. package/lib/typescript/utils/error-codes.d.ts.map +1 -0
  145. package/lib/typescript/utils/errors.d.ts +88 -225
  146. package/lib/typescript/utils/errors.d.ts.map +1 -1
  147. package/lib/typescript/utils/misc.d.ts +9 -4
  148. package/lib/typescript/utils/misc.d.ts.map +1 -1
  149. package/lib/typescript/utils/par.d.ts.map +1 -1
  150. package/lib/typescript/wallet-instance/index.d.ts +17 -1
  151. package/lib/typescript/wallet-instance/index.d.ts.map +1 -1
  152. package/lib/typescript/wallet-instance-attestation/issuing.d.ts.map +1 -1
  153. package/lib/typescript/wallet-instance-attestation/types.d.ts +4 -4
  154. package/package.json +1 -1
  155. package/src/client/generated/wallet-provider.ts +43 -13
  156. package/src/client/index.ts +28 -15
  157. package/src/credential/issuance/03-start-user-authorization.ts +1 -1
  158. package/src/credential/issuance/04-complete-user-authorization.ts +21 -21
  159. package/src/credential/issuance/05-authorize-access.ts +7 -4
  160. package/src/credential/issuance/06-obtain-credential.ts +39 -39
  161. package/src/credential/issuance/README.md +8 -14
  162. package/src/credential/issuance/errors.ts +44 -0
  163. package/src/credential/issuance/index.ts +4 -2
  164. package/src/credential/issuance/types.ts +0 -8
  165. package/src/credential/presentation/01-start-flow.ts +1 -1
  166. package/src/credential/presentation/03-get-request-object.ts +3 -3
  167. package/src/credential/presentation/04-send-authorization-response.ts +3 -3
  168. package/src/credential/presentation/errors.ts +41 -0
  169. package/src/credential/presentation/index.ts +2 -0
  170. package/src/credential/status/02-status-attestation.ts +17 -25
  171. package/src/credential/status/README.md +5 -2
  172. package/src/credential/status/types.ts +0 -15
  173. package/src/sd-jwt/errors.ts +39 -0
  174. package/src/sd-jwt/index.ts +5 -8
  175. package/src/sd-jwt/verifier.ts +5 -5
  176. package/src/trust/index.ts +3 -3
  177. package/src/utils/decoder.ts +3 -3
  178. package/src/utils/error-codes.ts +50 -0
  179. package/src/utils/errors.ts +152 -476
  180. package/src/utils/misc.ts +20 -17
  181. package/src/utils/par.ts +3 -2
  182. package/src/wallet-instance/README.md +26 -5
  183. package/src/wallet-instance/index.ts +40 -18
  184. package/src/wallet-instance-attestation/README.md +8 -2
  185. package/src/wallet-instance-attestation/issuing.ts +28 -36
@@ -1,4 +1,16 @@
1
- import type { CredentialIssuerEntityConfiguration } from "../trust/types";
1
+ import type { ProblemDetail } from "../client/generated/wallet-provider";
2
+ import type { CredentialIssuerEntityConfiguration } from "../trust";
3
+ import {
4
+ IssuerResponseErrorCodes,
5
+ WalletProviderResponseErrorCodes,
6
+ type IssuerResponseErrorCode,
7
+ type WalletProviderResponseErrorCode,
8
+ } from "./error-codes";
9
+
10
+ export { IssuerResponseErrorCodes, WalletProviderResponseErrorCodes };
11
+
12
+ // An error reason that supports both a string and a generic JSON object
13
+ type GenericErrorReason = string | Record<string, unknown>;
2
14
 
3
15
  /**
4
16
  * utility to format a set of attributes into an error message string
@@ -11,10 +23,15 @@ import type { CredentialIssuerEntityConfiguration } from "../trust/types";
11
23
  * @returns a human-readable serialization of the set
12
24
  */
13
25
  export const serializeAttrs = (
14
- attrs: Record<string, string | string>
26
+ attrs: Record<string, GenericErrorReason | number | Array<string> | undefined>
15
27
  ): string =>
16
28
  Object.entries(attrs)
17
- .map(([k, v]) => [k, Array.isArray(v) ? `(${v.join(", ")})` : v])
29
+ .filter(([, v]) => v !== undefined)
30
+ .map(([k, v]) => {
31
+ if (Array.isArray(v)) return [k, `(${v.join(", ")})`];
32
+ if (typeof v !== "string") return [k, JSON.stringify(v)];
33
+ return [k, v];
34
+ })
18
35
  .map((_) => _.join("="))
19
36
  .join(" ");
20
37
 
@@ -30,57 +47,20 @@ export const serializeAttrs = (
30
47
  * ```
31
48
  */
32
49
  export class IoWalletError extends Error {
33
- /** A unique error code for the particular error subclass. */
34
- static get code(): string {
35
- return "ERR_IO_WALLET_GENERIC";
36
- }
37
-
38
50
  /** A unique error code for the particular error subclass. */
39
51
  code: string = "ERR_IO_WALLET_GENERIC";
40
52
 
41
53
  constructor(message?: string) {
42
54
  super(message);
43
55
  this.name = this.constructor.name;
44
- // @ts-ignore
45
- Error.captureStackTrace?.(this, this.constructor);
46
56
  }
47
57
  }
48
58
 
49
- /**
50
- * An error subclass thrown when a Wallet Provider http request has a status code different from the one expected.
51
- */
52
- export class UnexpectedStatusCodeError extends IoWalletError {
53
- static get code(): "ERR_UNEXPECTED_STATUS_CODE" {
54
- return "ERR_UNEXPECTED_STATUS_CODE";
55
- }
56
-
57
- code = "ERR_UNEXPECTED_STATUS_CODE";
58
-
59
- /** HTTP status code */
60
- statusCode: number;
61
- /** The stringified response body, useful to process the error response */
62
- responseBody: string;
63
-
64
- constructor(message: string, statusCode: number, responseBody: string) {
65
- super(
66
- serializeAttrs({
67
- message,
68
- statusCode: statusCode.toString(),
69
- })
70
- );
71
- this.statusCode = statusCode;
72
- this.responseBody = responseBody;
73
- }
74
- }
75
59
  /**
76
60
  * An error subclass thrown when validation fail
77
61
  *
78
62
  */
79
63
  export class ValidationFailed extends IoWalletError {
80
- static get code(): "ERR_IO_WALLET_VALIDATION_FAILED" {
81
- return "ERR_IO_WALLET_VALIDATION_FAILED";
82
- }
83
-
84
64
  code = "ERR_IO_WALLET_VALIDATION_FAILED";
85
65
 
86
66
  /** The Claim for which the validation failed. */
@@ -89,95 +69,15 @@ export class ValidationFailed extends IoWalletError {
89
69
  /** Reason code for the validation failure. */
90
70
  reason: string;
91
71
 
92
- constructor(
93
- message: string,
94
- claim: string = "unspecified",
95
- reason: string = "unspecified"
96
- ) {
97
- super(serializeAttrs({ message, claim, reason }));
98
- this.claim = claim;
99
- this.reason = reason;
100
- }
101
- }
102
-
103
- /**
104
- * An error subclass thrown when validation fail
105
- *
106
- */
107
- export class WalletInstanceAttestationIssuingError extends IoWalletError {
108
- static get code(): "ERR_IO_WALLET_INSTANCE_ATTESTATION_ISSUING_FAILED" {
109
- return "ERR_IO_WALLET_INSTANCE_ATTESTATION_ISSUING_FAILED";
110
- }
111
-
112
- code = "ERR_IO_WALLET_INSTANCE_ATTESTATION_ISSUING_FAILED";
113
-
114
- /** The Claim for which the validation failed. */
115
- claim: string;
116
-
117
- /** Reason code for the validation failure. */
118
- reason: string;
119
-
120
- constructor(
121
- message: string,
122
- claim: string = "unspecified",
123
- reason: string = "unspecified"
124
- ) {
125
- super(serializeAttrs({ message, claim, reason }));
126
- this.claim = claim;
127
- this.reason = reason;
128
- }
129
- }
130
-
131
- /**
132
- * An error subclass thrown when auth request decode fail
133
- *
134
- */
135
- export class AuthRequestDecodeError extends IoWalletError {
136
- static get code(): "ERR_IO_WALLET_AUTHENTICATION_REQUEST_DECODE_FAILED" {
137
- return "ERR_IO_WALLET_AUTHENTICATION_REQUEST_DECODE_FAILED";
138
- }
139
-
140
- code = "ERR_IO_WALLET_AUTHENTICATION_REQUEST_DECODE_FAILED";
141
-
142
- /** The Claim for which the validation failed. */
143
- claim: string;
144
-
145
- /** Reason code for the validation failure. */
146
- reason: string;
147
-
148
- constructor(
149
- message: string,
150
- claim: string = "unspecified",
151
- reason: string = "unspecified"
152
- ) {
153
- super(serializeAttrs({ message, claim, reason }));
154
- this.claim = claim;
155
- this.reason = reason;
156
- }
157
- }
158
-
159
- /**
160
- * An error subclass thrown when validation fail
161
- *
162
- */
163
- export class PidIssuingError extends IoWalletError {
164
- static get code(): "ERR_IO_WALLET_PID_ISSUING_FAILED" {
165
- return "ERR_IO_WALLET_PID_ISSUING_FAILED";
166
- }
167
-
168
- code = "ERR_IO_WALLET_PID_ISSUING_FAILED";
169
-
170
- /** The Claim for which the validation failed. */
171
- claim: string;
172
-
173
- /** Reason code for the validation failure. */
174
- reason: string;
175
-
176
- constructor(
177
- message: string,
178
- claim: string = "unspecified",
179
- reason: string = "unspecified"
180
- ) {
72
+ constructor({
73
+ message,
74
+ claim = "unspecified",
75
+ reason = "unspecified",
76
+ }: {
77
+ message: string;
78
+ claim?: string;
79
+ reason?: string;
80
+ }) {
181
81
  super(serializeAttrs({ message, claim, reason }));
182
82
  this.claim = claim;
183
83
  this.reason = reason;
@@ -185,364 +85,67 @@ export class PidIssuingError extends IoWalletError {
185
85
  }
186
86
 
187
87
  /**
188
- * When claims are requested but not found in the credential
189
- *
190
- */
191
- export class ClaimsNotFoundBetweenDislosures extends Error {
192
- static get code(): "ERR_CLAIMS_NOT_FOUND" {
193
- return "ERR_CLAIMS_NOT_FOUND";
194
- }
195
-
196
- code = "ERR_CLAIMS_NOT_FOUND";
197
-
198
- /** The Claims not found */
199
- claims: string[];
200
-
201
- constructor(claims: string | string[]) {
202
- const c = Array.isArray(claims) ? claims : [claims];
203
- const message = `Some requested claims are not present in the disclosurable values, claims: ${c.join(
204
- ", "
205
- )}`;
206
- super(message);
207
- this.claims = c;
208
- }
209
- }
210
-
211
- /**
212
- * When the SD-JWT does not contain an hashed reference to a given set of claims
213
- */
214
- export class ClaimsNotFoundInToken extends Error {
215
- static get code(): "ERR_CLAIMS_NOT_FOUND_IN_TOKEN" {
216
- return "ERR_CLAIMS_NOT_FOUND_IN_TOKEN";
217
- }
218
-
219
- code = "ERR_CLAIMS_NOT_FOUND_IN_TOKEN";
220
-
221
- /** The Claims not found */
222
- claims: string[];
223
-
224
- constructor(claims: string | string[]) {
225
- const c = Array.isArray(claims) ? claims : [claims];
226
- const message = `Some claims are not found in the given token, claims: ${c.join(
227
- ", "
228
- )}`;
229
- super(message);
230
- this.claims = c;
231
- }
232
- }
233
-
234
- /**
235
- * When selecting a public key from an entity configuration, and no one meets the requirements for the scenario
236
- *
237
- */
238
- export class NoSuitableKeysFoundInEntityConfiguration extends Error {
239
- static get code(): "ERR_NO_SUITABLE_KEYS_NOT_FOUND" {
240
- return "ERR_NO_SUITABLE_KEYS_NOT_FOUND";
241
- }
242
-
243
- code = "ERR_NO_SUITABLE_KEYS_NOT_FOUND";
244
-
245
- /**
246
- * @param scenario describe the scenario in which the error arise
247
- */
248
- constructor(scenario: string) {
249
- const message = `Entity configuration do not provide any suitable keys (${scenario}).`;
250
- super(message);
251
- }
252
- }
253
-
254
- /**
255
- * When selecting a public key from an entity configuration, and no one meets the requirements for the scenario
256
- *
88
+ * An error subclass thrown when an HTTP request has a status code different from the one expected.
257
89
  */
258
- export class PidMetadataError extends Error {
259
- static get code(): "PID_METADATA_ERROR" {
260
- return "PID_METADATA_ERROR";
261
- }
262
-
263
- constructor(message: string) {
264
- super(message);
265
- }
266
- }
267
-
268
- /**
269
- * An error subclass thrown when a Wallet Provider http request fail
270
- *
271
- */
272
- export class WalletProviderResponseError extends IoWalletError {
273
- static get code(): "ERR_IO_WALLET_PROVIDER_RESPONSE_FAILED" {
274
- return "ERR_IO_WALLET_PROVIDER_RESPONSE_FAILED";
275
- }
276
-
277
- code = "ERR_IO_WALLET_PROVIDER_RESPONSE_FAILED";
278
-
279
- /** The Claim for which the validation failed. */
280
- claim: string;
281
-
282
- /** Reason code for the validation failure. */
283
- reason: string;
284
-
285
- /** HTTP status code */
90
+ export class UnexpectedStatusCodeError extends IoWalletError {
91
+ code: string = "ERR_UNEXPECTED_STATUS_CODE";
286
92
  statusCode: number;
93
+ reason: GenericErrorReason;
287
94
 
288
- constructor(
289
- message: string,
290
- claim: string = "unspecified",
291
- reason: string = "unspecified",
292
- statusCode: number
293
- ) {
294
- super(
295
- serializeAttrs({
296
- message,
297
- claim,
298
- reason,
299
- statusCode: statusCode.toString(),
300
- })
301
- );
302
- this.claim = claim;
95
+ constructor({
96
+ message,
97
+ reason,
98
+ statusCode,
99
+ }: {
100
+ message: string;
101
+ reason: GenericErrorReason;
102
+ statusCode: number;
103
+ }) {
104
+ super(serializeAttrs({ message, reason, statusCode }));
303
105
  this.reason = reason;
304
106
  this.statusCode = statusCode;
305
107
  }
306
108
  }
307
109
 
308
- export class WalletInstanceRevokedError extends IoWalletError {
309
- static get code(): "ERR_IO_WALLET_INSTANCE_REVOKED" {
310
- return "ERR_IO_WALLET_INSTANCE_REVOKED";
311
- }
312
-
313
- code = "ERR_IO_WALLET_INSTANCE_REVOKED";
314
-
315
- claim: string;
316
- reason: string;
317
-
318
- constructor(message: string, claim: string, reason: string = "unspecified") {
319
- super(serializeAttrs({ message, claim, reason }));
320
- this.reason = reason;
321
- this.claim = claim;
322
- }
323
- }
324
-
325
- export class WalletInstanceNotFoundError extends IoWalletError {
326
- static get code(): "ERR_IO_WALLET_INSTANCE_NOT_FOUND" {
327
- return "ERR_IO_WALLET_INSTANCE_NOT_FOUND";
328
- }
329
-
330
- code = "ERR_IO_WALLET_INSTANCE_NOT_FOUND";
331
-
332
- claim: string;
333
- reason: string;
334
-
335
- constructor(message: string, claim: string, reason: string = "unspecified") {
336
- super(serializeAttrs({ message, claim, reason }));
337
- this.reason = reason;
338
- this.claim = claim;
339
- }
340
- }
341
-
342
- /**
343
- * An error subclass thrown when obtaining a wallet instance attestation which fails due to the integrity.
344
- */
345
- export class WalletInstanceIntegrityFailedError extends IoWalletError {
346
- static get code(): "ERR_IO_WALLET_INSTANCE_INTEGRITY_FAILED" {
347
- return "ERR_IO_WALLET_INSTANCE_INTEGRITY_FAILED";
348
- }
349
-
350
- code = "ERR_IO_WALLET_INSTANCE_INTEGRITY_FAILED";
351
-
352
- reason: string;
353
-
354
- claim: string;
355
-
356
- constructor(message: string, claim: string, reason: string = "unspecified") {
357
- super(serializeAttrs({ message, claim, reason }));
358
- this.reason = reason;
359
- this.claim = claim;
360
- }
361
- }
362
-
363
- /**
364
- * An error subclass thrown when an error occurs during the wallet instance creation process.
365
- */
366
- export class WalletInstanceCreationError extends IoWalletError {
367
- static get code(): "ERR_IO_WALLET_INSTANCE_CREATION_ERROR" {
368
- return "ERR_IO_WALLET_INSTANCE_CREATION_ERROR";
369
- }
370
-
371
- code = "ERR_IO_WALLET_INSTANCE_CREATION_ERROR";
372
-
373
- /** The Claim for which the validation failed. */
374
- claim: string;
375
-
376
- /** Reason code for the validation failure. */
377
- reason: string;
378
-
379
- constructor(
380
- message: string,
381
- claim: string = "unspecified",
382
- reason: string = "unspecified"
383
- ) {
384
- super(serializeAttrs({ message, claim, reason }));
385
- this.claim = claim;
386
- this.reason = reason;
387
- }
388
- }
389
-
390
- /**
391
- * An error subclass thrown when obtaining a wallet instance attestation which fails due to the integrity.
392
- */
393
- export class WalletInstanceCreationIntegrityError extends IoWalletError {
394
- static get code(): "ERR_IO_WALLET_INSTANCE_CREATION_INTEGRITY_ERROR" {
395
- return "ERR_IO_WALLET_INSTANCE_CREATION_INTEGRITY_ERROR";
396
- }
397
-
398
- code = "ERR_IO_WALLET_INSTANCE_CREATION_INTEGRITY_ERROR";
399
-
400
- reason: string;
401
-
402
- claim: string;
403
-
404
- constructor(message: string, claim: string, reason: string = "unspecified") {
405
- super(serializeAttrs({ message, claim, reason }));
406
- this.reason = reason;
407
- this.claim = claim;
408
- }
409
- }
410
-
411
- /**
412
- * An error subclass thrown when an error occurs during the authorization process.
413
- */
414
- export class AuthorizationError extends IoWalletError {
415
- static get code(): "ERR_IO_WALLET_AUTHORIZATION_ERROR" {
416
- return "ERR_IO_WALLET_AUTHORIZATION_ERROR";
417
- }
418
-
419
- code = "ERR_IO_WALLET_AUTHORIZATION_ERROR";
420
-
421
- constructor(message?: string) {
422
- super(message);
423
- }
424
- }
425
-
426
110
  /**
427
- * An error subclass thrown when an error occurs during the authorization process with the IDP.
428
- * It contains the error and error description returned by the IDP.
429
- */
430
- export class AuthorizationIdpError extends IoWalletError {
431
- static get code(): "ERR_IO_WALLET_IDENTIFICATION_RESPONSE_ERROR" {
432
- return "ERR_IO_WALLET_IDENTIFICATION_RESPONSE_ERROR";
433
- }
434
-
435
- code = "ERR_IO_WALLET_IDENTIFICATION_RESPONSE_PARSING_FAILED";
436
-
437
- error: string;
438
- errorDescription?: string;
439
-
440
- constructor(error: string, errorDescription?: string) {
441
- super(
442
- serializeAttrs(errorDescription ? { error, errorDescription } : { error })
443
- );
444
- this.error = error;
445
- this.errorDescription = errorDescription;
446
- }
447
- }
448
-
449
- /**
450
- * Error subclass thrown when an operation has been aborted.
451
- */
452
- export class OperationAbortedError extends IoWalletError {
453
- static get code(): "ERR_IO_WALLET_OPERATION_ABORTED" {
454
- return "ERR_IO_WALLET_OPERATION_ABORTED";
455
- }
456
-
457
- code = "ERR_IO_WALLET_OPERATION_ABORTED";
458
-
459
- /** The aborted operation */
460
- operation: string;
461
-
462
- constructor(operation: string) {
463
- super(serializeAttrs({ operation }));
464
- this.operation = operation;
465
- }
466
- }
467
-
468
- /**
469
- * Error subclass thrown when a credential status is invalid, either during issuance or when requesting a status attestation.
470
- */
471
- export class CredentialInvalidStatusError extends IoWalletError {
472
- static get code(): "ERR_CREDENTIAL_INVALID_STATUS" {
473
- return "ERR_CREDENTIAL_INVALID_STATUS";
474
- }
475
-
476
- code = "ERR_CREDENTIAL_INVALID_STATUS";
477
-
478
- /**
479
- * The error code that should be mapped with one of the `issuance_errors_supported` in the EC.
480
- */
481
- errorCode: string;
482
- reason: string;
483
-
484
- constructor(
485
- message: string,
486
- errorCode: string,
487
- reason: string = "unspecified"
488
- ) {
489
- super(serializeAttrs({ message, errorCode, reason }));
490
- this.errorCode = errorCode;
491
- this.reason = reason;
492
- }
493
- }
494
-
495
- /**
496
- * Error subclass thrown when an error occurs while obtaining a status attestation for a credential.
497
- */
498
- export class StatusAttestationError extends IoWalletError {
499
- static get code(): "ERR_STATUS_ATTESTATION_ERROR" {
500
- return "ERR_STATUS_ATTESTATION_ERROR";
501
- }
502
-
503
- code = "ERR_STATUS_ATTESTATION_ERROR";
504
-
505
- reason: string;
506
-
507
- constructor(message: string, reason: string = "unspecified") {
508
- super(serializeAttrs({ message, reason }));
509
- this.reason = reason;
510
- }
511
- }
512
-
513
- /**
514
- * Error subclass thrown when an error occurs while requesting a credential.
111
+ * An error subclass thrown when an Issuer HTTP request fails.
112
+ * The specific error can be found in the `code` property.
113
+ *
114
+ * The class is generic over the error code to narrow down the reason.
515
115
  */
516
- export class CredentialRequestError extends IoWalletError {
517
- static get code(): "CREDENTIAL_REQUEST_ERROR" {
518
- return "CREDENTIAL_REQUEST_ERROR";
519
- }
520
-
521
- code = "CREDENTIAL_REQUEST_ERROR";
522
-
523
- reason: string;
524
-
525
- constructor(message: string, reason: string = "unspecified") {
526
- super(serializeAttrs({ message, reason }));
527
- this.reason = reason;
116
+ export class IssuerResponseError extends UnexpectedStatusCodeError {
117
+ code: IssuerResponseErrorCode;
118
+
119
+ constructor(params: {
120
+ code?: IssuerResponseErrorCode;
121
+ message: string;
122
+ reason: GenericErrorReason;
123
+ statusCode: number;
124
+ }) {
125
+ super(params);
126
+ this.code = params.code ?? IssuerResponseErrorCodes.IssuerGenericError;
528
127
  }
529
128
  }
530
129
 
531
130
  /**
532
- * Error subclass thrown when a credential cannot be issued immediately because it follows the async flow.
131
+ * An error subclass thrown when a Wallet Provider HTTP request fails.
132
+ * The specific error can be found in the `code` property.
533
133
  */
534
- export class CredentialIssuingNotSynchronousError extends IoWalletError {
535
- static get code(): "CREDENTIAL_ISSUING_NOT_SYNCHRONOUS_ERROR" {
536
- return "CREDENTIAL_ISSUING_NOT_SYNCHRONOUS_ERROR";
537
- }
538
-
539
- code = "CREDENTIAL_ISSUING_NOT_SYNCHRONOUS_ERROR";
540
-
541
- reason: string;
542
-
543
- constructor(message: string, reason: string = "unspecified") {
544
- super(serializeAttrs({ message, reason }));
545
- this.reason = reason;
134
+ export class WalletProviderResponseError extends UnexpectedStatusCodeError {
135
+ code: WalletProviderResponseErrorCode;
136
+ reason: ProblemDetail;
137
+
138
+ constructor(params: {
139
+ code?: WalletProviderResponseErrorCode;
140
+ message: string;
141
+ reason: ProblemDetail;
142
+ statusCode: number;
143
+ }) {
144
+ super(params);
145
+ this.reason = params.reason;
146
+ this.code =
147
+ params.code ??
148
+ WalletProviderResponseErrorCodes.WalletProviderGenericError;
546
149
  }
547
150
  }
548
151
 
@@ -556,10 +159,10 @@ type LocalizedIssuanceError = {
556
159
  /**
557
160
  * Function to extract the error message from the Entity Configuration's supported error codes.
558
161
  * @param errorCode The error code to map to a meaningful message
559
- * @param params.issuerConf The entity configuration for credentials
560
- * @param params.credentialType The type of credential the error belongs to
162
+ * @param issuerConf The entity configuration for credentials
163
+ * @param credentialType The type of credential the error belongs to
561
164
  * @returns A localized error {@link LocalizedIssuanceError} or undefined
562
- * @throws {Error} When no credential config is found
165
+ * @throws {IoWalletError} When no credential config is found
563
166
  */
564
167
  export function extractErrorMessageFromIssuerConf(
565
168
  errorCode: string,
@@ -577,7 +180,7 @@ export function extractErrorMessageFromIssuerConf(
577
180
  ];
578
181
 
579
182
  if (!credentialConfiguration) {
580
- throw new Error(
183
+ throw new IoWalletError(
581
184
  `No configuration found for ${credentialType} in the provided EC`
582
185
  );
583
186
  }
@@ -595,3 +198,76 @@ export function extractErrorMessageFromIssuerConf(
595
198
  {} as LocalizedIssuanceError
596
199
  );
597
200
  }
201
+
202
+ /**
203
+ * Type guard for issuer errors.
204
+ * @param error The error to check
205
+ * @param code Optional code to narrow down the issuer error
206
+ */
207
+ export const isIssuerResponseError = (
208
+ error: unknown,
209
+ code?: IssuerResponseErrorCode
210
+ ): error is IssuerResponseError =>
211
+ error instanceof IssuerResponseError && error.code === (code ?? error.code);
212
+
213
+ /**
214
+ * Type guard for wallet provider errors.
215
+ * @param error The error to check
216
+ * @param code Optional code to narrow down the wallet provider error
217
+ */
218
+ export const isWalletProviderResponseError = (
219
+ error: unknown,
220
+ code?: WalletProviderResponseErrorCode
221
+ ): error is WalletProviderResponseError =>
222
+ error instanceof WalletProviderResponseError &&
223
+ error.code === (code ?? error.code);
224
+
225
+ type ErrorCodeMap<T> = T extends typeof IssuerResponseError
226
+ ? IssuerResponseErrorCode
227
+ : T extends typeof WalletProviderResponseError
228
+ ? WalletProviderResponseErrorCode
229
+ : never;
230
+
231
+ type ErrorCase<T> = {
232
+ code: ErrorCodeMap<T>;
233
+ message: string;
234
+ reason?: GenericErrorReason;
235
+ };
236
+
237
+ /**
238
+ * Builder class used to create specialized errors from type {@link UnexpectedStatusCodeError} that handles multiple status codes.
239
+ *
240
+ * Chain multiple `handle` to add cases that depend on the status code, then call `buildFrom` when done.
241
+ *
242
+ * For example:
243
+ * ```
244
+ * new ResponseErrorBuilder(IssuerResponseError)
245
+ * .handle(403, { code: "ERROR_CODE_1", message: "Forbidden" })
246
+ * .handle(500, { code: "ERROR_CODE_2", message: "Unexpected error" })
247
+ * .handle("*", { code: "ERROR_CODE_3", message: "Fallback" })
248
+ * .buildFrom(baseError)
249
+ * ```
250
+ */
251
+ export class ResponseErrorBuilder<T extends typeof UnexpectedStatusCodeError> {
252
+ private errorCases: {
253
+ [K in number | "*"]?: ErrorCase<T>;
254
+ } = {};
255
+
256
+ constructor(private ErrorClass: T) {}
257
+
258
+ handle(status: number | "*", params: ErrorCase<T>) {
259
+ this.errorCases[status] = params;
260
+ return this;
261
+ }
262
+
263
+ buildFrom(originalError: UnexpectedStatusCodeError) {
264
+ const params =
265
+ this.errorCases[originalError.statusCode] ?? this.errorCases["*"];
266
+
267
+ if (params) {
268
+ return new this.ErrorClass({ ...originalError, ...params });
269
+ }
270
+
271
+ return originalError;
272
+ }
273
+ }