feat(errors): add centralized exception handling

This commit is contained in:
2026-07-03 12:55:45 +08:00
parent e852fb62e9
commit 65d972fbb4
20 changed files with 492 additions and 70 deletions
+47
View File
@@ -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);
}
}