Skip to content

Commit 89941a2

Browse files
reecefenwickReece Fenwickmichaelangeloio
authored
feat(ts-rest/nest): http exception options in TsRestException (#445)
Co-authored-by: Reece Fenwick <reecefenwick@Reeces-MacBook-Pro.local> Co-authored-by: Michael Angelo Rivera <55844504+michaelangeloio@users.noreply.github.com>
1 parent 3aeeb40 commit 89941a2

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

.changeset/metal-mugs-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/nest': minor
3+
---
4+
5+
feat: `@ts-rest/nest` Adds support to provide a `cause` to `TsRestException`

libs/ts-rest/nest/src/lib/ts-rest-nest-handler.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,4 +1337,38 @@ describe('ts-rest-nest-handler', () => {
13371337
}),
13381338
});
13391339
});
1340+
1341+
it('should support including a nested error as the cause', async () => {
1342+
const c = initContract();
1343+
1344+
const contract = c.router({
1345+
getPosts: {
1346+
path: '/posts',
1347+
method: 'GET',
1348+
query: z.object({
1349+
limit: z.string(),
1350+
}),
1351+
responses: {
1352+
200: z.array(
1353+
z.object({
1354+
id: z.number(),
1355+
})
1356+
),
1357+
},
1358+
},
1359+
});
1360+
1361+
const cause = new Error('the root cause');
1362+
1363+
const error = new TsRestException(contract.getPosts, {
1364+
status: 400,
1365+
body: {
1366+
message: 'Something went wrong'
1367+
}
1368+
}, {
1369+
cause
1370+
})
1371+
1372+
expect(error.cause).toEqual(cause);
1373+
})
13401374
});

libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ExecutionContext,
1212
Get,
1313
HttpException,
14+
HttpExceptionOptions,
1415
Injectable,
1516
InternalServerErrorException,
1617
NestInterceptor,
@@ -165,8 +166,8 @@ export const tsRestHandler = <T extends AppRouter | AppRoute>(
165166
* Error you can throw to return a response from a handler
166167
*/
167168
export class TsRestException<T extends AppRoute> extends HttpException {
168-
constructor(route: T, response: ServerInferResponses<T>) {
169-
super(response.body as Record<string, any>, response.status);
169+
constructor(route: T, response: ServerInferResponses<T>, options?: HttpExceptionOptions) {
170+
super(response.body as Record<string, any>, response.status, options);
170171
}
171172
}
172173

0 commit comments

Comments
 (0)