Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update SMS messaging parse error #855

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions lib/base/RestException.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
"use strict";
interface RestExceptionError {
status: number;
message: string;
code: number;
moreInfo: string;
details: object;
}

export default class RestException extends Error {
export default class RestException extends Error implements RestExceptionError {
status: number;
message: string;
code: number;
Expand All @@ -9,15 +16,28 @@ export default class RestException extends Error {

constructor(response) {
super("[HTTP " + response.statusCode + "] Failed to execute request");
const isResponseBodyString = typeof response.body == "string";
const body = isResponseBodyString
? parseResponseBody(response.body)
: response.body;

const body =
typeof response.body === "string"
? JSON.parse(response.body)
: response.body;
this.status = response.statusCode;
this.message = body.message;
this.code = body.code;
this.moreInfo = body.more_info; /* jshint ignore:line */
this.details = body.details;
if (body !== null) {
this.message = body.message;
this.code = body.code;
this.moreInfo = body.more_info; /* jshint ignore:line */
this.details = body.details;
}
}
}

function parseResponseBody(response_body: string): RestExceptionError | null {
let body = null;
try {
body = JSON.parse(response_body);
} catch (catchError) {
body = null;
}

return body;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe("exception gets created from string", function () {
statusCode: 200,
body: '{"message":"test", "code":81022,"more_info": "https://www.twilio.com/docs/errors/81022"}',
};

const exception = new RestException(response);
expect(exception.status).toEqual(200);
expect(exception.message).toEqual("test");
Expand All @@ -14,6 +15,19 @@ describe("exception gets created from string", function () {
"https://www.twilio.com/docs/errors/81022"
);
});
it("should test serialize from improper json string", function () {
const response = {
statusCode: 200,
body: '{message":test", "code:81022,"more_info": "https://www.twilio.com/docs/errors/81022"}',
};
const exception = new RestException(response);
expect(exception.status).toEqual(200);
expect(exception.message).toEqual(
`[HTTP ${response.statusCode}] Failed to execute request`
);
expect(exception.code).toEqual(undefined);
expect(exception.moreInfo).toEqual(undefined);
});
});

describe("exception gets created from json error", function () {
Expand Down