feat: Enrich error responses
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
14
packages/backend/src/errors/create-auth-data.ts
Normal file
14
packages/backend/src/errors/create-auth-data.ts
Normal 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!`;
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
import BaseError from './base';
|
||||
|
||||
export default class HttpError extends BaseError {}
|
12
packages/backend/src/errors/http.ts
Normal file
12
packages/backend/src/errors/http.ts
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user