Skip to content

Commit f6fafb3

Browse files
author
Kewin Polok
committed
Throw ErrorResponse instead of returning
1 parent 72c676f commit f6fafb3

File tree

5 files changed

+56
-35
lines changed

5 files changed

+56
-35
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function isMessageErrorResponseData(
2+
response: any
3+
): response is MessageErrorResponse {
4+
return response.error !== undefined && response.message !== undefined;
5+
}
6+
7+
export interface MessageErrorResponse {
8+
error: number;
9+
message: string;
10+
}
11+
12+
class MessageError extends Error {
13+
public data: MessageErrorResponse;
14+
15+
constructor(data: MessageErrorResponse) {
16+
super();
17+
18+
Error.captureStackTrace(this, this.constructor);
19+
20+
this.name = 'MessageError';
21+
this.data = data;
22+
}
23+
}
24+
25+
export { MessageError };

src/modules/baseMessageModule/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import snakeCase from 'lodash/snakeCase';
77

88
import { BaseModule } from '../baseModule';
99
import { SmsDetails } from '../sms/types/SmsDetails';
10-
import { ErrorResponse, isErrorResponse } from '../../types/ErrorResponse';
1110
import { MessageResponse } from '../../types/MessageResponse';
11+
import {
12+
MessageError,
13+
MessageErrorResponse,
14+
isMessageErrorResponseData,
15+
} from '../../errors/MessageResponseError';
1216

1317
import {
1418
MessageContent,
@@ -40,7 +44,7 @@ export class BaseMessageModule extends BaseModule {
4044
content: MessageContent,
4145
recipient: Recipient,
4246
details?: SmsDetails
43-
): Promise<MessageResponse | ErrorResponse> {
47+
): Promise<MessageResponse> {
4448
const body: Record<string, unknown> = {
4549
details: true,
4650
encoding: 'utf-8',
@@ -81,22 +85,29 @@ export class BaseMessageModule extends BaseModule {
8185
if (this.isVmsLocalFile(content)) {
8286
const formData = this.getFormDataForVmsLocalFile(body, content);
8387

84-
const data = await this.httpClient.post<MessageResponse, MessageResponse>(
85-
this.endpoint,
86-
formData.getBuffer(),
87-
{
88-
headers: formData.getHeaders(),
89-
}
90-
);
88+
const data = await this.httpClient.post<
89+
MessageResponse | MessageErrorResponse,
90+
MessageResponse | MessageErrorResponse
91+
>(this.endpoint, formData.getBuffer(), {
92+
headers: formData.getHeaders(),
93+
});
94+
95+
if (isMessageErrorResponseData(data)) {
96+
throw new MessageError(data);
97+
}
9198

9299
return this.formatResponse(data);
93100
}
94101

95102
const data = await this.httpClient.post<
96-
MessageResponse | ErrorResponse,
97-
MessageResponse | ErrorResponse
103+
MessageResponse | MessageErrorResponse,
104+
MessageResponse | MessageErrorResponse
98105
>(this.endpoint, body);
99106

107+
if (isMessageErrorResponseData(data)) {
108+
throw new MessageError(data);
109+
}
110+
100111
return this.formatResponse(data);
101112
}
102113

@@ -193,13 +204,7 @@ export class BaseMessageModule extends BaseModule {
193204
});
194205
}
195206

196-
protected formatResponse(
197-
response: MessageResponse | ErrorResponse
198-
): MessageResponse | ErrorResponse {
199-
if (isErrorResponse(response)) {
200-
return response;
201-
}
202-
207+
protected formatResponse(response: MessageResponse): MessageResponse {
203208
return {
204209
...response,
205210
list: response.list.map((sms) => ({

src/modules/vms/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BaseMessageModule } from '../baseMessageModule';
22
import { MessageResponse } from '../../types/MessageResponse';
3-
import { ErrorResponse } from '../../types/ErrorResponse';
43

54
import { VmsTtsLector } from './types/VmsTtsLector';
65
import { VmsDetails } from './types/VmsDetails';
@@ -13,7 +12,7 @@ export class Vms extends BaseMessageModule {
1312
tts: string,
1413
ttsLector?: VmsTtsLector,
1514
details?: VmsDetails
16-
): Promise<MessageResponse | ErrorResponse> {
15+
): Promise<MessageResponse> {
1716
return await this.send(
1817
{
1918
tts,
@@ -30,7 +29,7 @@ export class Vms extends BaseMessageModule {
3029
numbers: string | string[],
3130
pathToLocaleFile: string,
3231
details?: VmsDetails
33-
): Promise<MessageResponse | ErrorResponse> {
32+
): Promise<MessageResponse> {
3433
return await this.send(
3534
{
3635
localPath: pathToLocaleFile,
@@ -46,7 +45,7 @@ export class Vms extends BaseMessageModule {
4645
numbers: string | string[],
4746
pathToRemoteFile: string,
4847
details?: VmsDetails
49-
): Promise<MessageResponse | ErrorResponse> {
48+
): Promise<MessageResponse> {
5049
return await this.send(
5150
{
5251
remotePath: pathToRemoteFile,
@@ -63,7 +62,7 @@ export class Vms extends BaseMessageModule {
6362
tts: string,
6463
ttsLector?: VmsTtsLector,
6564
details?: VmsDetails
66-
): Promise<MessageResponse | ErrorResponse> {
65+
): Promise<MessageResponse> {
6766
return await this.send(
6867
{
6968
tts,
@@ -80,7 +79,7 @@ export class Vms extends BaseMessageModule {
8079
groups: string | string[],
8180
pathToLocaleFile: string,
8281
details?: VmsDetails
83-
): Promise<MessageResponse | ErrorResponse> {
82+
): Promise<MessageResponse> {
8483
return await this.send(
8584
{
8685
localPath: pathToLocaleFile,
@@ -96,7 +95,7 @@ export class Vms extends BaseMessageModule {
9695
groups: string | string[],
9796
pathToRemoteFile: string,
9897
details?: VmsDetails
99-
): Promise<MessageResponse | ErrorResponse> {
98+
): Promise<MessageResponse> {
10099
return await this.send(
101100
{
102101
remotePath: pathToRemoteFile,

src/types/ErrorResponse.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import { SubuserCredentials } from '../modules/subusers/types/SubuserCredentials
1515
import { SubuserPoints } from '../modules/subusers/types/SubuserPoints';
1616
import { Template } from '../modules/templates/types/Template';
1717
import { UpdateSubuser } from '../modules/subusers/types/UpdateSubuser';
18+
import { MessageErrorResponse } from '../errors/MessageResponseError';
1819

19-
import { ErrorResponse } from './ErrorResponse';
2020
import { MessageStatus } from './MessageStatus';
2121
import { MessageResponse } from './MessageResponse';
2222
import { ApiCollection } from './ApiCollection';
2323

2424
export {
2525
ApiCollection,
26-
ErrorResponse,
2726
HlrCheck,
2827
HlrCheckError,
2928
HlrCheckResponse,
29+
MessageErrorResponse,
3030
MessageResponse,
3131
MessageStatus,
3232
MmsDetails,

0 commit comments

Comments
 (0)