Skip to content

Commit 5d49036

Browse files
committed
Added error handling with decorator
1 parent f220298 commit 5d49036

File tree

13 files changed

+277
-419
lines changed

13 files changed

+277
-419
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const withExceptionCatch = (_target: any, _key: string, descriptor: PropertyDescriptor) => {
2+
const originalMethod = descriptor.value
3+
4+
descriptor.value = async function (...args: any[]) {
5+
try {
6+
return await originalMethod.apply(this, args)
7+
} catch (error) {
8+
console.log(error)
9+
return {
10+
statusCode: 503,
11+
message: "Service unavailable"
12+
}
13+
}
14+
}
15+
16+
return descriptor
17+
}

source/api/v1/exceptions/AuthExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export const AUTH_EXCEPTIONS = {
1111
},
1212
ServiceUnavailable: {
1313
statusCode: 503,
14-
message: "Cannot create auth, service unavalable"
14+
message: "Auth service unavalable"
1515
},
1616
} as const satisfies Record<string, Exception>

source/api/v1/exceptions/NoteExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const NOTE_EXCEPTIONS = {
1515
},
1616
ServiceUnavailable: {
1717
statusCode: 503,
18-
message: "Notes service is unavailable"
18+
message: "Notes service unavailable"
1919
},
2020
AcessRestricted: {
2121
statusCode: 403,

source/api/v1/exceptions/UserExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const USER_EXCEPTIONS = {
88

99
ServiceUnavailable: {
1010
statusCode: 503,
11-
message: "Cannot create user, service unavalable"
11+
message: "Users service unavalable"
1212
},
1313

1414
NotFound: {

source/api/v1/handlers/AuthHandlers.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { AUTH_EXCEPTIONS } from "../exceptions/AuthExceptions";
55
import { AuthUserSchema, ChangePasswordSchema } from "../validation/schemas/AuthSchemas";
66
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
77
import { extractToken } from "../utils/TokenExtractor";
8+
import { isException } from "../utils/guards/ExceptionGuard";
9+
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
810

911
export const handleAuthRoutes = (
1012
server: FastifyInstance,
@@ -20,42 +22,40 @@ export const handleAuthRoutes = (
2022
Reply: {
2123
200: { token: string, expiresIn: string },
2224
400: typeof AUTH_EXCEPTIONS.WrongCredentials,
23-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable
25+
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
2426
}
2527
}>("/auth", {
2628
schema: AuthUserSchema
2729
}, async (request, reply) => {
28-
try {
2930
const credentials: UserCredentials = request.body
3031

31-
const [token, expiresIn] = await authService.authorizeAndGetToken(
32+
const result = await authService.authorizeAndGetToken(
3233
credentials.email,
3334
credentials.password
34-
) as [string, string]
35+
)
36+
if (isException(result)) {
37+
reply.code(result.statusCode).send(result)
38+
return
39+
}
3540

3641
reply.code(200).send({
37-
token,
38-
expiresIn
42+
token: result[0],
43+
expiresIn: result[1]
3944
})
40-
} catch (exception: any) {
41-
reply.code(
42-
exception.statusCode
43-
).send(exception)
44-
}
45+
4546
})
4647

4748
server.patch<{
4849
Body: { oldPassword: string, newPassword: string },
4950
Reply: {
5051
200: { success: true },
5152
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame,
52-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable
53+
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
5354
}
5455
}>("/auth/password", {
5556
schema: ChangePasswordSchema,
5657
preHandler: authenticate
5758
}, async (request, reply) => {
58-
try {
5959
const passwords = request.body
6060
const payload = extractJwtPayload(
6161
extractToken(request)
@@ -64,13 +64,13 @@ export const handleAuthRoutes = (
6464
payload.login,
6565
passwords.oldPassword,
6666
passwords.newPassword
67-
) as { success: true }
67+
)
68+
if (isException(state)) {
69+
reply.code(state.statusCode).send(state)
70+
return
71+
}
6872

6973
reply.code(200).send(state)
70-
} catch (exception: any) {
71-
reply.code(
72-
exception.statusCode
73-
).send(exception)
74-
}
74+
7575
})
7676
}

source/api/v1/handlers/NotesHandlers.ts

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { NOTE_EXCEPTIONS } from "../exceptions/NoteExceptions";
55
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
66
import { extractToken } from "../utils/TokenExtractor";
77
import {AddCollaboratorSchema, CreateNoteSchema, DeleteNoteSchema, GetNoteCollaboratorsSchema, GetNoteSchema, GetNotesSchema, RemoveCollaboratorSchema, UpdateNoteSchema } from "../validation/schemas/NoteSchemas";
8+
import { isException } from "../utils/guards/ExceptionGuard";
9+
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
810

911

1012
export const handleNoteRoutes = (
@@ -20,14 +22,13 @@ export const handleNoteRoutes = (
2022
Body: Omit<NoteWithoutMetadata, "author">,
2123
Reply: {
2224
201: Note,
23-
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable,
25+
503: typeof NOTE_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
2426
404: typeof NOTE_EXCEPTIONS.CollaboratorNotFound
2527
}
2628
}>("/notes", {
2729
schema: CreateNoteSchema,
2830
preHandler: authenticate
2931
}, async (request, reply) => {
30-
try {
3132
const payload = extractJwtPayload(
3233
extractToken(request)
3334
)
@@ -36,14 +37,14 @@ export const handleNoteRoutes = (
3637
...request.body,
3738
author: payload.login
3839
}
39-
console.log(123)
40-
const createdNote = await notesService.createNote(insertData) as Note
40+
const createdNote = await notesService.createNote(insertData)
41+
if (isException(createdNote)) {
42+
reply.code(createdNote.statusCode).send(createdNote)
43+
return
44+
}
45+
4146
reply.code(201).send(createdNote)
42-
} catch (exception: any) {
43-
reply.code(
44-
exception.statusCode
45-
).send(exception)
46-
}
47+
4748
})
4849

4950
server.get<{
@@ -63,7 +64,6 @@ export const handleNoteRoutes = (
6364
preHandler: authenticate
6465
},
6566
async (request, reply) => {
66-
try {
6767
const payload = extractJwtPayload(
6868
extractToken(request)
6969
)
@@ -73,13 +73,14 @@ export const handleNoteRoutes = (
7373
const sort = request.query.sort
7474
const tags = request.query.tags
7575

76-
const notes = await notesService.getMyNotes(payload.login, tags, limit, skip, sort) as NotePreview[]
76+
const notes = await notesService.getMyNotes(payload.login, tags, limit, skip, sort)
77+
if (isException(notes)) {
78+
reply.code(notes.statusCode).send(notes)
79+
return
80+
}
81+
7782
reply.code(200).send(notes)
78-
} catch (exception: any) {
79-
reply.code(
80-
exception.statusCode
81-
).send(exception)
82-
}
83+
8384
})
8485

8586
server.get<{
@@ -97,7 +98,6 @@ export const handleNoteRoutes = (
9798
schema: GetNotesSchema,
9899
preHandler: authenticate
99100
}, async (request, reply) => {
100-
try {
101101
const payload = extractJwtPayload(
102102
extractToken(request)
103103
)
@@ -107,13 +107,12 @@ export const handleNoteRoutes = (
107107
const sort = request.query.sort
108108
const tags = request.query.tags
109109

110-
const notes = await notesService.getCollaboratedNotes(payload.login, tags, limit, skip, sort) as NotePreview[]
110+
const notes = await notesService.getCollaboratedNotes(payload.login, tags, limit, skip, sort)
111+
if (isException(notes)) {
112+
reply.code(notes.statusCode).send(notes)
113+
return
114+
}
111115
reply.code(200).send(notes)
112-
} catch (exception: any) {
113-
reply.code(
114-
exception.statusCode
115-
).send(exception)
116-
}
117116
})
118117

119118
server.get<{
@@ -129,20 +128,19 @@ export const handleNoteRoutes = (
129128
preHandler: authenticate
130129
},
131130
async (request, reply) => {
132-
try {
133131
const payload = extractJwtPayload(
134132
extractToken(request)
135133
)
136134

137135
const id = request.params.id
138136

139-
const foundNote = await notesService.getNote(id, payload.login) as Note
137+
const foundNote = await notesService.getNote(id, payload.login)
138+
if (isException(foundNote)) {
139+
reply.code(foundNote.statusCode).send(foundNote)
140+
return
141+
}
140142
reply.code(200).send(foundNote)
141-
} catch (exception: any) {
142-
reply.code(
143-
exception.statusCode
144-
).send(exception)
145-
}
143+
146144
})
147145

148146
server.delete<{
@@ -158,20 +156,19 @@ export const handleNoteRoutes = (
158156
preHandler: authenticate
159157
},
160158
async (request, reply) => {
161-
try {
162159
const payload = extractJwtPayload(
163160
extractToken(request)
164161
)
165162

166163
const id = request.params.id
167164

168-
const state = await notesService.deleteNote(id, payload.login) as {success: true}
165+
const state = await notesService.deleteNote(id, payload.login)
166+
if (isException(state)) {
167+
reply.code(state.statusCode).send(state)
168+
return
169+
}
169170
reply.code(200).send(state)
170-
} catch (exception: any) {
171-
reply.code(
172-
exception.statusCode
173-
).send(exception)
174-
}
171+
175172
})
176173

177174
server.patch<{
@@ -186,21 +183,21 @@ export const handleNoteRoutes = (
186183
schema: UpdateNoteSchema,
187184
preHandler: authenticate
188185
}, async (request, reply) => {
189-
try {
190186
const payload = extractJwtPayload(
191187
extractToken(request)
192188
)
193189

194190
const id = request.params.id
195191
const updateData = request.body
196192

197-
const updatedNote = await notesService.updateNote(id, payload.login, updateData) as Note
193+
const updatedNote = await notesService.updateNote(id, payload.login, updateData)
194+
if (isException(updatedNote)) {
195+
reply.code(updatedNote.statusCode).send(updatedNote)
196+
return
197+
}
198+
198199
reply.code(200).send(updatedNote)
199-
} catch (exception: any) {
200-
reply.code(
201-
exception.statusCode
202-
).send(exception)
203-
}
200+
204201
})
205202

206203
server.get<{
@@ -214,19 +211,17 @@ export const handleNoteRoutes = (
214211
schema: GetNoteCollaboratorsSchema,
215212
preHandler: authenticate
216213
}, async (request, reply) => {
217-
try {
218214
const payload = extractJwtPayload(
219215
extractToken(request)
220216
)
221217
const id = request.params.id
222-
const collaborators = await notesService.getCollaborators(id, payload.login) as NoteCollaborators
223-
218+
const collaborators = await notesService.getCollaborators(id, payload.login)
219+
if (isException(collaborators)) {
220+
reply.code(collaborators.statusCode).send(collaborators)
221+
return
222+
}
224223
reply.code(200).send(collaborators)
225-
} catch (exception: any) {
226-
reply.code(
227-
exception.statusCode
228-
).send(exception)
229-
}
224+
230225
})
231226

232227
server.put<{
@@ -245,7 +240,6 @@ export const handleNoteRoutes = (
245240
schema: AddCollaboratorSchema,
246241
preHandler: authenticate
247242
}, async (request, reply) => {
248-
try {
249243
const payload = extractJwtPayload(
250244
extractToken(request)
251245
)
@@ -257,14 +251,13 @@ export const handleNoteRoutes = (
257251
id,
258252
payload.login,
259253
collaboratorLogin
260-
) as { success: true }
254+
)
255+
if (isException(state)) {
256+
reply.code(state.statusCode).send(state)
257+
return
258+
}
261259

262260
reply.code(201).send(state)
263-
} catch (exception: any) {
264-
reply.code(
265-
exception.statusCode
266-
).send(exception)
267-
}
268261
})
269262

270263
server.delete<{
@@ -282,7 +275,6 @@ export const handleNoteRoutes = (
282275
schema: RemoveCollaboratorSchema,
283276
preHandler: authenticate
284277
}, async (request, reply) => {
285-
try {
286278
const payload = extractJwtPayload(
287279
extractToken(request)
288280
)
@@ -294,13 +286,13 @@ export const handleNoteRoutes = (
294286
id,
295287
payload.login,
296288
collaboratorLogin
297-
) as { success: true }
289+
)
290+
if (isException(state)) {
291+
reply.code(state.statusCode).send(state)
292+
return
293+
}
298294

299295
reply.code(200).send(state)
300-
} catch (exception: any) {
301-
reply.code(
302-
exception.statusCode
303-
).send(exception)
304-
}
296+
305297
})
306298
}

0 commit comments

Comments
 (0)