-
Notifications
You must be signed in to change notification settings - Fork 47
/
errors.ts
179 lines (135 loc) · 4.49 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { AxiosError } from "axios";
import { Payment } from "./collections";
import { FailureReason } from "./common";
import { Transfer } from "./disbursements";
interface ErrorBody {
code: FailureReason;
message: string;
}
export class MtnMoMoError extends Error {
public transaction?: Payment | Transfer;
constructor(message?: string) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
}
}
export class ApprovalRejectedError extends MtnMoMoError {
public name = "ApprovalRejectedError";
}
export class ExpiredError extends MtnMoMoError {
public name = "ExpiredError";
}
export class InternalProcessingError extends MtnMoMoError {
public name = "InternalProcessingError";
}
export class InvalidCallbackUrlHostError extends MtnMoMoError {
public name = "InvalidCallbackUrlHostError";
}
export class InvalidCurrencyError extends MtnMoMoError {
public name = "InvalidCurrencyError";
}
export class NotAllowedTargetEnvironmentError extends MtnMoMoError {
public name = "NotAllowedTargetEnvironmentError";
}
export class NotAllowedError extends MtnMoMoError {
public name = "NotAllowedError";
}
export class NotEnoughFundsError extends MtnMoMoError {
public name = "NotEnoughFundsError";
}
export class PayeeNotFoundError extends MtnMoMoError {
public name = "PayeeNotFoundError";
}
export class PayeeNotAllowedToReceiveError extends MtnMoMoError {
public name = "PayeeNotAllowedToReceiveError";
}
export class PayerLimitReachedError extends MtnMoMoError {
public name = "PayerLimitReachedError";
}
export class PayerNotFoundError extends MtnMoMoError {
public name = "PayerNotFoundError";
}
export class PaymentNotApprovedError extends MtnMoMoError {
public name = "PaymentNotApprovedError";
}
export class ResourceAlreadyExistError extends MtnMoMoError {
public name = "ResourceAlreadyExistError";
}
export class ResourceNotFoundError extends MtnMoMoError {
public name = "ResourceNotFoundError";
}
export class ServiceUnavailableError extends MtnMoMoError {
public name = "ServiceUnavailableError";
}
export class TransactionCancelledError extends MtnMoMoError {
public name = "TransactionCancelledError";
}
export class UnspecifiedError extends MtnMoMoError {
public name = "UnspecifiedError";
}
export function handleError(error: AxiosError): Error {
if (!error.response) {
return error;
}
const { code, message }: ErrorBody = error.response.data || {};
return getError(code, message);
}
export function getError(code?: FailureReason, message?: string) {
if (code === FailureReason.APPROVAL_REJECTED) {
return new ApprovalRejectedError(message);
}
if (code === FailureReason.EXPIRED) {
return new ExpiredError(message);
}
if (code === FailureReason.INTERNAL_PROCESSING_ERROR) {
return new InternalProcessingError(message);
}
if (code === FailureReason.INVALID_CALLBACK_URL_HOST) {
return new InvalidCallbackUrlHostError(message);
}
if (code === FailureReason.INVALID_CURRENCY) {
return new InvalidCurrencyError(message);
}
if (code === FailureReason.NOT_ALLOWED) {
return new NotAllowedError(message);
}
if (code === FailureReason.NOT_ALLOWED_TARGET_ENVIRONMENT) {
return new NotAllowedTargetEnvironmentError(message);
}
if (code === FailureReason.NOT_ENOUGH_FUNDS) {
return new NotEnoughFundsError(message);
}
if (code === FailureReason.PAYEE_NOT_FOUND) {
return new PayeeNotFoundError(message);
}
if (code === FailureReason.PAYEE_NOT_ALLOWED_TO_RECEIVE) {
return new PayeeNotAllowedToReceiveError(message);
}
if (code === FailureReason.PAYER_LIMIT_REACHED) {
return new PayerLimitReachedError(message);
}
if (code === FailureReason.PAYER_NOT_FOUND) {
return new PayerNotFoundError(message);
}
if (code === FailureReason.PAYMENT_NOT_APPROVED) {
return new PaymentNotApprovedError(message);
}
if (code === FailureReason.RESOURCE_ALREADY_EXIST) {
return new ResourceAlreadyExistError(message);
}
if (code === FailureReason.RESOURCE_NOT_FOUND) {
return new ResourceNotFoundError(message);
}
if (code === FailureReason.SERVICE_UNAVAILABLE) {
return new ServiceUnavailableError(message);
}
if (code === FailureReason.TRANSACTION_CANCELED) {
return new TransactionCancelledError(message);
}
return new UnspecifiedError(message);
}
export function getTransactionError(transaction: Payment | Transfer) {
const error: MtnMoMoError = getError(transaction.reason as FailureReason);
error.transaction = transaction;
return error;
}