Skip to content

Commit 6a71773

Browse files
committed
fix handler for default elysia
1 parent 3df3923 commit 6a71773

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

example/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ const app = new Elysia()
107107
throw new MisdirectedRequestException('Request was misdirected');
108108
})
109109
.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+
);
111113
})
112114
.get('/423', () => {
113115
throw new LockedException('The resource is locked');
@@ -175,9 +177,9 @@ const app = new Elysia()
175177
error: 'VALIDATION_FAILED',
176178
details: {
177179
field: 'email',
178-
message: 'Invalid email format'
180+
message: 'Invalid email format',
179181
},
180-
timestamp: new Date().toISOString()
182+
timestamp: new Date().toISOString(),
181183
});
182184
})
183185

@@ -193,50 +195,50 @@ const app = new Elysia()
193195
// Practical example - User management
194196
.get('/users/:id', ({ params }) => {
195197
const userId = parseInt(params.id);
196-
198+
197199
if (isNaN(userId)) {
198200
throw new BadRequestException('User ID must be a valid number');
199201
}
200-
202+
201203
if (userId < 1) {
202204
throw new BadRequestException('User ID must be positive');
203205
}
204-
206+
205207
if (userId === 404) {
206208
throw new NotFoundException(`User with ID ${userId} not found`);
207209
}
208-
210+
209211
if (userId === 403) {
210212
throw new ForbiddenException('You do not have permission to view this user');
211213
}
212-
214+
213215
return { id: userId, name: `User ${userId}`, email: `user${userId}@example.com` };
214216
})
215217

216218
// API rate limiting example
217219
.get('/api/data', ({ request }) => {
218220
const rateLimitExceeded = Math.random() > 0.7; // Simulate rate limiting
219-
221+
220222
if (rateLimitExceeded) {
221223
throw new TooManyRequestsException({
222224
message: 'Rate limit exceeded',
223225
retryAfter: 60,
224226
limit: 100,
225-
remaining: 0
227+
remaining: 0,
226228
});
227229
}
228-
230+
229231
return { data: 'Some API data', timestamp: Date.now() };
230232
})
231233

232234
// Database connection example
233235
.get('/database/status', () => {
234236
const dbConnected = Math.random() > 0.3; // Simulate database connection
235-
237+
236238
if (!dbConnected) {
237239
throw new ServiceUnavailableException('Database connection failed - please try again later');
238240
}
239-
241+
240242
return { status: 'Database is healthy', connections: 25 };
241243
})
242244

src/http-exception-plugin.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Elysia } from 'elysia';
22
import { HttpException } from './exceptions/http-exception';
3+
import { HttpError } from './types/http-error';
34

45
export const httpExceptionPlugin = () =>
56
new Elysia({ name: 'elysia-http-exception' })
@@ -18,14 +19,17 @@ export const httpExceptionPlugin = () =>
1819
headers: { 'Content-Type': 'application/json' },
1920
});
2021
})
21-
.onError({ as: 'scoped' }, ({ error, set }) => {
22+
.onError({ as: 'scoped' }, ({ code, error, set }) => {
2223
if (error instanceof HttpException) {
23-
set.headers['content-type'] = 'application/json; charset=utf-8'
24+
set.headers['content-type'] = 'application/json; charset=utf-8';
2425
set.status = error.statusCode;
2526
return error.toBody();
2627
}
27-
28-
set.status = 500;
29-
const message = error instanceof Error ? error.message : 'Internal server error';
30-
return { statusCode: 500, message };
28+
29+
if (code === 'NOT_FOUND') {
30+
const { code: httpCode, statusCode, message } = JSON.parse(HttpError.NOT_FOUND);
31+
set.headers['content-type'] = 'application/json; charset=utf-8';
32+
set.status = parseInt(httpCode, 10);
33+
return { statusCode, message };
34+
}
3135
});

0 commit comments

Comments
 (0)