Skip to content

Commit fba14f0

Browse files
refactor(api): simplify error handling (freeCodeCamp#58925)
1 parent 9875c13 commit fba14f0

File tree

2 files changed

+32
-68
lines changed

2 files changed

+32
-68
lines changed

api/src/routes/protected/settings.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,15 @@ Happy coding!
912912
expect(user?.location).toEqual('');
913913
expect(user?.picture).toEqual('');
914914
});
915+
916+
test('PUT returns 400 status code with invalid about settings', async () => {
917+
const response = await superPut('/update-my-about').send({
918+
about: { no: 'objects' }
919+
});
920+
921+
expect(response.body).toEqual(updateErrorResponse);
922+
expect(response.statusCode).toEqual(400);
923+
});
915924
});
916925

917926
describe('/update-my-honesty', () => {
@@ -1007,6 +1016,15 @@ Happy coding!
10071016
expect(response.statusCode).toEqual(200);
10081017
});
10091018

1019+
test('PUT returns 400 status code with invalid classroom mode', async () => {
1020+
const response = await superPut('/update-my-classroom-mode').send({
1021+
isClassroomAccount: 'invalid'
1022+
});
1023+
1024+
expect(response.body).toEqual(updateErrorResponse);
1025+
expect(response.statusCode).toEqual(400);
1026+
});
1027+
10101028
test('After updating the classroom mode, the user should have this property set', async () => {
10111029
await superPut('/update-my-classroom-mode').send({
10121030
isClassroomAccount: false

api/src/routes/protected/settings.ts

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
import {
2-
TypeBoxTypeProvider,
3-
type FastifyPluginCallbackTypebox
4-
} from '@fastify/type-provider-typebox';
5-
import type {
6-
ContextConfigDefault,
7-
FastifyError,
8-
FastifyInstance,
9-
FastifyReply,
10-
FastifyRequest,
11-
RawReplyDefaultExpression,
12-
RawRequestDefaultExpression,
13-
RawServerDefault,
14-
RouteGenericInterface
15-
} from 'fastify';
16-
import { ResolveFastifyReplyType } from 'fastify/types/type-provider';
1+
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
2+
import type { FastifyInstance } from 'fastify';
173
import { differenceInMinutes } from 'date-fns';
184
import validator from 'validator';
195

@@ -110,44 +96,19 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
11096
_options,
11197
done
11298
) => {
113-
type CommonResponseSchema = {
114-
response: { 400: (typeof schemas.updateMyProfileUI.response)[400] };
115-
};
116-
117-
// TODO: figure out if there's a nicer way to generate this type
118-
type UpdateReplyType = FastifyReply<
119-
RawServerDefault,
120-
RawRequestDefaultExpression<RawServerDefault>,
121-
RawReplyDefaultExpression<RawServerDefault>,
122-
RouteGenericInterface,
123-
ContextConfigDefault,
124-
CommonResponseSchema,
125-
TypeBoxTypeProvider,
126-
ResolveFastifyReplyType<
127-
TypeBoxTypeProvider,
128-
CommonResponseSchema,
129-
RouteGenericInterface
130-
>
131-
>;
132-
133-
function updateErrorHandler(
134-
error: FastifyError,
135-
request: FastifyRequest,
136-
reply: UpdateReplyType
137-
) {
99+
fastify.setErrorHandler((error, request, reply) => {
138100
if (error.validation) {
139101
void reply.code(400);
140102
void reply.send({ message: 'flash.wrong-updating', type: 'danger' });
141103
} else {
142-
fastify.errorHandler(error, request, reply);
104+
throw error;
143105
}
144-
}
106+
});
145107

146108
fastify.put(
147109
'/update-my-profileui',
148110
{
149-
schema: schemas.updateMyProfileUI,
150-
errorHandler: updateErrorHandler
111+
schema: schemas.updateMyProfileUI
151112
},
152113
async (req, reply) => {
153114
try {
@@ -325,8 +286,7 @@ ${isLinkSentWithinLimitTTL}`
325286
fastify.put(
326287
'/update-my-theme',
327288
{
328-
schema: schemas.updateMyTheme,
329-
errorHandler: updateErrorHandler
289+
schema: schemas.updateMyTheme
330290
},
331291
async (req, reply) => {
332292
try {
@@ -353,8 +313,7 @@ ${isLinkSentWithinLimitTTL}`
353313
fastify.put(
354314
'/update-my-socials',
355315
{
356-
schema: schemas.updateMySocials,
357-
errorHandler: updateErrorHandler
316+
schema: schemas.updateMySocials
358317
},
359318
async (req, reply) => {
360319
const valid = (['twitter', 'githubProfile', 'linkedin'] as const).every(
@@ -514,8 +473,7 @@ ${isLinkSentWithinLimitTTL}`
514473
fastify.put(
515474
'/update-my-keyboard-shortcuts',
516475
{
517-
schema: schemas.updateMyKeyboardShortcuts,
518-
errorHandler: updateErrorHandler
476+
schema: schemas.updateMyKeyboardShortcuts
519477
},
520478
async (req, reply) => {
521479
try {
@@ -542,8 +500,7 @@ ${isLinkSentWithinLimitTTL}`
542500
fastify.put(
543501
'/update-my-quincy-email',
544502
{
545-
schema: schemas.updateMyQuincyEmail,
546-
errorHandler: updateErrorHandler
503+
schema: schemas.updateMyQuincyEmail
547504
},
548505
async (req, reply) => {
549506
try {
@@ -570,8 +527,7 @@ ${isLinkSentWithinLimitTTL}`
570527
fastify.put(
571528
'/update-my-honesty',
572529
{
573-
schema: schemas.updateMyHonesty,
574-
errorHandler: updateErrorHandler
530+
schema: schemas.updateMyHonesty
575531
},
576532
async (req, reply) => {
577533
try {
@@ -598,8 +554,7 @@ ${isLinkSentWithinLimitTTL}`
598554
fastify.put(
599555
'/update-privacy-terms',
600556
{
601-
schema: schemas.updateMyPrivacyTerms,
602-
errorHandler: updateErrorHandler
557+
schema: schemas.updateMyPrivacyTerms
603558
},
604559
async (req, reply) => {
605560
try {
@@ -627,8 +582,7 @@ ${isLinkSentWithinLimitTTL}`
627582
fastify.put(
628583
'/update-my-portfolio',
629584
{
630-
schema: schemas.updateMyPortfolio,
631-
errorHandler: updateErrorHandler
585+
schema: schemas.updateMyPortfolio
632586
},
633587
async (req, reply) => {
634588
try {
@@ -666,15 +620,7 @@ ${isLinkSentWithinLimitTTL}`
666620
fastify.put(
667621
'/update-my-classroom-mode',
668622
{
669-
schema: schemas.updateMyClassroomMode,
670-
errorHandler: (error, request, reply) => {
671-
if (error.validation) {
672-
void reply.code(403);
673-
void reply.send({ message: 'flash.wrong-updating', type: 'danger' });
674-
} else {
675-
fastify.errorHandler(error, request, reply);
676-
}
677-
}
623+
schema: schemas.updateMyClassroomMode
678624
},
679625
async (req, reply) => {
680626
try {

0 commit comments

Comments
 (0)