UNPKG

1.19 kBJavaScriptView Raw
1export default class M2ApiResponseError extends Error {
2 constructor({ method, resourceUrl, response, bodyText }, ...args) {
3 let body = ``;
4 try {
5 const { message, trace, ...rest } = JSON.parse(bodyText);
6 if (message) {
7 body += `Message:\n\n ${message}\n`;
8 }
9 const addl = Object.entries(rest);
10 if (addl.length > 0) {
11 body += `\nAdditional info:\n\n${JSON.stringify(
12 rest,
13 null,
14 4
15 )}\n\n`;
16 }
17 if (trace) {
18 body += `Magento PHP stack trace: \n\n${trace}`;
19 }
20 body += '\n';
21 } catch (e) {
22 body = bodyText;
23 }
24 super(
25 `${method} ${resourceUrl} responded ${response.status} ${
26 response.statusText
27 }: \n\n${body}`,
28 ...args
29 );
30 if (Error.captureStackTrace) {
31 Error.captureStackTrace(this, M2ApiResponseError);
32 }
33 this.response = response;
34 this.method = method;
35 this.resourceUrl = resourceUrl;
36 }
37}