feat: Enrich error responses

This commit is contained in:
Faruk AYDIN
2022-11-07 22:51:30 +01:00
parent ad46b3eea3
commit 06a2014bc1
13 changed files with 97 additions and 101 deletions

View File

@@ -1,14 +1,14 @@
import { IJSONObject } from '@automatisch/types';
export default class BaseError extends Error {
error = {};
details = {};
constructor(error?: string | IJSONObject) {
let computedError: Record<string, unknown>;
try {
computedError = JSON.parse(error as string);
} catch {
computedError = typeof error === 'string' ? { error: computedError } : error;
computedError = typeof error === 'string' ? { error } : error;
}
let computedMessage: string;
@@ -26,7 +26,7 @@ export default class BaseError extends Error {
super(computedMessage);
this.error = computedError;
this.details = computedError;
this.name = this.constructor.name;
}
}

View File

@@ -0,0 +1,14 @@
import { IJSONObject } from '@automatisch/types';
import BaseError from './base';
export default class CreateAuthDataError extends BaseError {
constructor(error: IJSONObject) {
const computedError =
((error.response as IJSONObject)?.data as IJSONObject) ||
(error.message as string);
super(computedError);
this.message = `Error occured while creating authorization URL!`;
}
}

View File

@@ -1,3 +0,0 @@
import BaseError from './base';
export default class HttpError extends BaseError {}

View File

@@ -0,0 +1,12 @@
import { IJSONObject } from '@automatisch/types';
import BaseError from './base';
export default class HttpError extends BaseError {
constructor(error: IJSONObject) {
const computedError =
((error.response as IJSONObject)?.data as IJSONObject) ||
(error.message as string);
super(computedError);
}
}