@bytegigs/daraja 0.0.0-stage → 0.3.0

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.
package/dist/errors.js ADDED
@@ -0,0 +1,42 @@
1
+ /** Daraja answered with an error, or could not be reached. */
2
+ export class DarajaError extends Error {
3
+ status;
4
+ errorCode;
5
+ requestId;
6
+ retryAfterMs;
7
+ responseBody;
8
+ name = "DarajaError";
9
+ constructor(message, status, errorCode, requestId,
10
+ /** From Daraja's Retry-After header, when it sends one. */
11
+ retryAfterMs = null,
12
+ /** The start of Daraja's response body, for errors that don't follow its usual shape. */
13
+ responseBody = null) {
14
+ super(message);
15
+ this.status = status;
16
+ this.errorCode = errorCode;
17
+ this.requestId = requestId;
18
+ this.retryAfterMs = retryAfterMs;
19
+ this.responseBody = responseBody;
20
+ }
21
+ /** Daraja's gateway is rate-limiting this app (HTTP 429): wait and retry with backoff. */
22
+ get isRateLimited() {
23
+ return this.status === 429;
24
+ }
25
+ /** Your Daraja app doesn't include this API product (HTTP 401 "no apiproduct match found"). */
26
+ get isProductMissing() {
27
+ return this.status === 401 && /no apiproduct match/i.test(this.message);
28
+ }
29
+ /** True while a customer is still answering an STK prompt (`500.001.1001`). */
30
+ get isPending() {
31
+ return this.errorCode === "500.001.1001";
32
+ }
33
+ }
34
+ /** The request was rejected before it was sent, because Daraja would reject it too. */
35
+ export class DarajaValidationError extends Error {
36
+ field;
37
+ name = "DarajaValidationError";
38
+ constructor(field, message) {
39
+ super(`${field}: ${message}`);
40
+ this.field = field;
41
+ }
42
+ }