Skip to content

Commit f14ad97

Browse files
authored
fix: return errors from fastify route handlers (#326)
* add test case for erroring fastify route handlers * re-throw other errors in requestValidationErrorHandler * Add changeset
1 parent 55411ad commit f14ad97

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

.changeset/late-panthers-pump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/fastify': patch
3+
---
4+
5+
Re-throw errors from route handlers

libs/ts-rest/fastify/src/lib/ts-rest-fastify.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,33 @@ describe('ts-rest-fastify', () => {
389389
expect(responseCss.text).toEqual('body { color: red; }');
390390
expect(responseCss.header['content-type']).toEqual('text/css');
391391
});
392+
393+
it('should return errors from route handlers', async () => {
394+
const erroringRouter = s.router(contract, {
395+
test: async () => {
396+
throw new Error('not implemented');
397+
},
398+
ping: async () => {
399+
throw new Error('not implemented');
400+
},
401+
testPathParams: async () => {
402+
throw new Error('not implemented');
403+
},
404+
returnsTheWrongData: async () => {
405+
throw new Error('not implemented');
406+
},
407+
});
408+
409+
const app = fastify({ logger: false });
410+
411+
s.registerRouter(contract, erroringRouter, app);
412+
413+
await app.ready();
414+
415+
const response = await supertest(app.server)
416+
.get('/test')
417+
.timeout(1000)
418+
.send({});
419+
expect(response.statusCode).toEqual(500);
420+
});
392421
});

libs/ts-rest/fastify/src/lib/ts-rest-fastify.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ const requestValidationErrorHandler = (
176176
} else {
177177
return handler(err, request, reply);
178178
}
179+
} else {
180+
throw err;
179181
}
180182
};
181183
};

0 commit comments

Comments
 (0)