@tristeroresearch/mach-sdk
Version:
A TypeScript SDK for integrating with Mach's API.
50 lines (49 loc) • 1.66 kB
JavaScript
/**
* @module httpError
* @description HTTP error class
*/
import { BaseError } from './baseError.js';
import { MachErrorCode } from './constants.js';
import { ErrorName } from './constants.js';
const statusCodeToErrorClassificationMap = new Map([
[
400,
{ type: ErrorName.ValidationError, code: MachErrorCode.ValidationError },
],
[404, { type: ErrorName.NotFoundError, code: MachErrorCode.NotFound }],
[500, { type: ErrorName.ServerError, code: MachErrorCode.InternalError }],
]);
const getErrorClassificationFromStatusCode = (code) => statusCodeToErrorClassificationMap.get(code) ?? {
type: ErrorName.ServerError,
code: MachErrorCode.InternalError,
};
export class HTTPError extends BaseError {
response;
status;
url;
type;
responseBody;
constructor(response, url) {
const errorClassification = getErrorClassificationFromStatusCode(response.status);
super(ErrorName.HTTPError, errorClassification.code, 'HTTP Error');
this.type = errorClassification.type;
this.response = response;
this.status = response.status;
this.url = url;
}
async buildAdditionalDetails() {
if (this.type) {
this.message = `[${this.type}] ${this.message}`;
}
try {
this.responseBody = await this.response.json();
if (this.responseBody) {
this.message += this.message.endsWith('.')
? ` ${this.responseBody?.message.toString()}`
: `. ${this.responseBody?.message.toString()}`;
}
}
catch { }
return this;
}
}