feat(errors): add centralized exception handling
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { AppException } from "./app-exception";
|
||||
import type { ExceptionResult } from "./exception-result";
|
||||
import { ApiExceptionHandler } from "./handlers/api-exception-handler";
|
||||
import { FallbackExceptionHandler } from "./handlers/fallback-exception-handler";
|
||||
import { NetworkExceptionHandler } from "./handlers/network-exception-handler";
|
||||
import { PaymentExceptionHandler } from "./handlers/payment-exception-handler";
|
||||
import { StorageExceptionHandler } from "./handlers/storage-exception-handler";
|
||||
import { ValidationExceptionHandler } from "./handlers/validation-exception-handler";
|
||||
|
||||
export class ExceptionHandler {
|
||||
static handle(error: unknown): ExceptionResult {
|
||||
if (error instanceof AppException) {
|
||||
return error.toResult();
|
||||
}
|
||||
|
||||
if (ApiExceptionHandler.canHandle(error)) {
|
||||
return ApiExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
if (NetworkExceptionHandler.canHandle(error)) {
|
||||
return NetworkExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
if (ValidationExceptionHandler.canHandle(error)) {
|
||||
return ValidationExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
if (StorageExceptionHandler.canHandle(error)) {
|
||||
return StorageExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
if (PaymentExceptionHandler.canHandle(error)) {
|
||||
return PaymentExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
return FallbackExceptionHandler.handle(error);
|
||||
}
|
||||
|
||||
static message(error: unknown): string {
|
||||
return ExceptionHandler.handle(error).message;
|
||||
}
|
||||
|
||||
static toError(error: unknown): AppException {
|
||||
if (error instanceof AppException) return error;
|
||||
return new AppException(ExceptionHandler.handle(error), error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user