@@ -107,7 +107,9 @@ const app = new Elysia()
107
107
throw new MisdirectedRequestException ( 'Request was misdirected' ) ;
108
108
} )
109
109
. get ( '/422' , ( ) => {
110
- throw new UnprocessableEntityException ( 'The request was well-formed but contains semantic errors' ) ;
110
+ throw new UnprocessableEntityException (
111
+ 'The request was well-formed but contains semantic errors' ,
112
+ ) ;
111
113
} )
112
114
. get ( '/423' , ( ) => {
113
115
throw new LockedException ( 'The resource is locked' ) ;
@@ -175,9 +177,9 @@ const app = new Elysia()
175
177
error : 'VALIDATION_FAILED' ,
176
178
details : {
177
179
field : 'email' ,
178
- message : 'Invalid email format'
180
+ message : 'Invalid email format' ,
179
181
} ,
180
- timestamp : new Date ( ) . toISOString ( )
182
+ timestamp : new Date ( ) . toISOString ( ) ,
181
183
} ) ;
182
184
} )
183
185
@@ -193,50 +195,50 @@ const app = new Elysia()
193
195
// Practical example - User management
194
196
. get ( '/users/:id' , ( { params } ) => {
195
197
const userId = parseInt ( params . id ) ;
196
-
198
+
197
199
if ( isNaN ( userId ) ) {
198
200
throw new BadRequestException ( 'User ID must be a valid number' ) ;
199
201
}
200
-
202
+
201
203
if ( userId < 1 ) {
202
204
throw new BadRequestException ( 'User ID must be positive' ) ;
203
205
}
204
-
206
+
205
207
if ( userId === 404 ) {
206
208
throw new NotFoundException ( `User with ID ${ userId } not found` ) ;
207
209
}
208
-
210
+
209
211
if ( userId === 403 ) {
210
212
throw new ForbiddenException ( 'You do not have permission to view this user' ) ;
211
213
}
212
-
214
+
213
215
return { id : userId , name : `User ${ userId } ` , email : `user${ userId } @example.com` } ;
214
216
} )
215
217
216
218
// API rate limiting example
217
219
. get ( '/api/data' , ( { request } ) => {
218
220
const rateLimitExceeded = Math . random ( ) > 0.7 ; // Simulate rate limiting
219
-
221
+
220
222
if ( rateLimitExceeded ) {
221
223
throw new TooManyRequestsException ( {
222
224
message : 'Rate limit exceeded' ,
223
225
retryAfter : 60 ,
224
226
limit : 100 ,
225
- remaining : 0
227
+ remaining : 0 ,
226
228
} ) ;
227
229
}
228
-
230
+
229
231
return { data : 'Some API data' , timestamp : Date . now ( ) } ;
230
232
} )
231
233
232
234
// Database connection example
233
235
. get ( '/database/status' , ( ) => {
234
236
const dbConnected = Math . random ( ) > 0.3 ; // Simulate database connection
235
-
237
+
236
238
if ( ! dbConnected ) {
237
239
throw new ServiceUnavailableException ( 'Database connection failed - please try again later' ) ;
238
240
}
239
-
241
+
240
242
return { status : 'Database is healthy' , connections : 25 } ;
241
243
} )
242
244
0 commit comments