11// Centralized Error Handling
22export enum ErrorCode {
3- SEAL_NOT_FOUND = ' SEAL_NOT_FOUND' ,
4- SEAL_LOCKED = ' SEAL_LOCKED' ,
5- INVALID_KEY = ' INVALID_KEY' ,
6- INVALID_INPUT = ' INVALID_INPUT' ,
7- DECRYPTION_FAILED = ' DECRYPTION_FAILED' ,
8- RATE_LIMIT_EXCEEDED = ' RATE_LIMIT_EXCEEDED' ,
9- INVALID_UNLOCK_TIME = ' INVALID_UNLOCK_TIME' ,
10- STORAGE_ERROR = ' STORAGE_ERROR' ,
11- DATABASE_ERROR = ' DATABASE_ERROR' ,
12- INTERNAL_ERROR = ' INTERNAL_ERROR' ,
3+ SEAL_NOT_FOUND = " SEAL_NOT_FOUND" ,
4+ SEAL_LOCKED = " SEAL_LOCKED" ,
5+ INVALID_KEY = " INVALID_KEY" ,
6+ INVALID_INPUT = " INVALID_INPUT" ,
7+ DECRYPTION_FAILED = " DECRYPTION_FAILED" ,
8+ RATE_LIMIT_EXCEEDED = " RATE_LIMIT_EXCEEDED" ,
9+ INVALID_UNLOCK_TIME = " INVALID_UNLOCK_TIME" ,
10+ STORAGE_ERROR = " STORAGE_ERROR" ,
11+ DATABASE_ERROR = " DATABASE_ERROR" ,
12+ INTERNAL_ERROR = " INTERNAL_ERROR" ,
1313}
1414
1515export interface AppError {
@@ -22,61 +22,73 @@ export interface AppError {
2222export const ErrorMessages : Record < ErrorCode , AppError > = {
2323 [ ErrorCode . SEAL_NOT_FOUND ] : {
2424 code : ErrorCode . SEAL_NOT_FOUND ,
25- message : 'Seal does not exist' ,
26- userMessage : 'This seal could not be found. Please check the link and try again.' ,
25+ message : "Seal does not exist" ,
26+ userMessage :
27+ "This seal could not be found. Please check the link and try again." ,
2728 statusCode : 404 ,
2829 } ,
2930 [ ErrorCode . SEAL_LOCKED ] : {
3031 code : ErrorCode . SEAL_LOCKED ,
31- message : 'Seal is still locked' ,
32- userMessage : 'This seal is still locked. Please wait until the unlock time.' ,
32+ message : "Seal is still locked" ,
33+ userMessage :
34+ "This seal is still locked. Please wait until the unlock time." ,
3335 statusCode : 403 ,
3436 } ,
3537 [ ErrorCode . INVALID_KEY ] : {
3638 code : ErrorCode . INVALID_KEY ,
37- message : 'Invalid decryption key' ,
38- userMessage : 'The decryption key is invalid. Make sure you have the correct link.' ,
39+ message : "Invalid decryption key" ,
40+ userMessage :
41+ "The decryption key is invalid. Make sure you have the correct link." ,
3942 statusCode : 400 ,
4043 } ,
4144 [ ErrorCode . INVALID_INPUT ] : {
4245 code : ErrorCode . INVALID_INPUT ,
43- message : ' Invalid input provided' ,
44- userMessage : ' The provided input is invalid. Please check and try again.' ,
46+ message : " Invalid input provided" ,
47+ userMessage : " The provided input is invalid. Please check and try again." ,
4548 statusCode : 400 ,
4649 } ,
4750 [ ErrorCode . DECRYPTION_FAILED ] : {
4851 code : ErrorCode . DECRYPTION_FAILED ,
49- message : ' Decryption failed' ,
50- userMessage : ' Failed to decrypt the seal. The data may be corrupted.' ,
52+ message : " Decryption failed" ,
53+ userMessage : " Failed to decrypt the seal. The data may be corrupted." ,
5154 statusCode : 500 ,
5255 } ,
5356 [ ErrorCode . RATE_LIMIT_EXCEEDED ] : {
5457 code : ErrorCode . RATE_LIMIT_EXCEEDED ,
55- message : ' Too many requests' ,
56- userMessage : ' Too many requests. Please wait a moment and try again.' ,
58+ message : " Too many requests" ,
59+ userMessage : " Too many requests. Please wait a moment and try again." ,
5760 statusCode : 429 ,
5861 } ,
5962 [ ErrorCode . INVALID_UNLOCK_TIME ] : {
6063 code : ErrorCode . INVALID_UNLOCK_TIME ,
61- message : ' Invalid unlock time' ,
62- userMessage : ' The unlock time must be in the future.' ,
64+ message : " Invalid unlock time" ,
65+ userMessage : " The unlock time must be in the future." ,
6366 statusCode : 400 ,
6467 } ,
6568 [ ErrorCode . STORAGE_ERROR ] : {
6669 code : ErrorCode . STORAGE_ERROR ,
67- message : ' Storage operation failed' ,
68- userMessage : ' Failed to store the seal. Please try again.' ,
70+ message : " Storage operation failed" ,
71+ userMessage : " Failed to store the seal. Please try again." ,
6972 statusCode : 500 ,
7073 } ,
7174 [ ErrorCode . DATABASE_ERROR ] : {
7275 code : ErrorCode . DATABASE_ERROR ,
73- message : 'Database operation failed' ,
74- userMessage : 'A database error occurred. Please try again.' ,
76+ message : "Database operation failed" ,
77+ userMessage : "A database error occurred. Please try again." ,
78+ statusCode : 500 ,
79+ } ,
80+ [ ErrorCode . INTERNAL_ERROR ] : {
81+ code : ErrorCode . INTERNAL_ERROR ,
82+ message : "Internal server error" ,
83+ userMessage : "An unexpected error occurred. Please try again." ,
7584 statusCode : 500 ,
7685 } ,
7786} ;
7887
79- export function createErrorResponse ( code : ErrorCode , details ?: string ) : Response {
88+ export function createErrorResponse (
89+ code : ErrorCode ,
90+ details ?: string ,
91+ ) : Response {
8092 const error = ErrorMessages [ code ] ;
8193 return new Response (
8294 JSON . stringify ( {
@@ -88,33 +100,22 @@ export function createErrorResponse(code: ErrorCode, details?: string): Response
88100 } ) ,
89101 {
90102 status : error . statusCode ,
91- headers : { ' Content-Type' : ' application/json' } ,
92- }
103+ headers : { " Content-Type" : " application/json" } ,
104+ } ,
93105 ) ;
94106}
95107
96108export function handleError ( error : unknown ) : Response {
97- console . error ( ' Error:' , error ) ;
98-
109+ console . error ( " Error:" , error ) ;
110+
99111 if ( error instanceof Error ) {
100- if ( error . message . includes ( ' not found' ) ) {
112+ if ( error . message . includes ( " not found" ) ) {
101113 return createErrorResponse ( ErrorCode . SEAL_NOT_FOUND ) ;
102114 }
103- if ( error . message . includes ( ' decrypt' ) ) {
115+ if ( error . message . includes ( " decrypt" ) ) {
104116 return createErrorResponse ( ErrorCode . DECRYPTION_FAILED ) ;
105117 }
106118 }
107-
108- return new Response (
109- JSON . stringify ( {
110- error : {
111- code : 'INTERNAL_ERROR' ,
112- message : 'An unexpected error occurred. Please try again.' ,
113- } ,
114- } ) ,
115- {
116- status : 500 ,
117- headers : { 'Content-Type' : 'application/json' } ,
118- }
119- ) ;
119+
120+ return createErrorResponse ( ErrorCode . INTERNAL_ERROR ) ;
120121}
0 commit comments