1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-11 00:10:06 +00:00

chore: improve error handling

This commit is contained in:
2022-06-27 23:21:48 +05:00
parent a75edbaa32
commit 0a73a35547
4 changed files with 91 additions and 33 deletions

View File

@@ -14,7 +14,9 @@ permissionRouter.get('/', authenticateAccessToken, async (req, res) => {
const response = await controller.getAllPermissions()
res.send(response)
} catch (err: any) {
res.status(403).send(err.toString())
const statusCode = err.code
delete err.code
res.status(statusCode).send(err.message)
}
})
@@ -30,7 +32,9 @@ permissionRouter.post(
const response = await controller.createPermission(body)
res.send(response)
} catch (err: any) {
res.status(403).send(err.toString())
const statusCode = err.code
delete err.code
res.status(statusCode).send(err.message)
}
}
)
@@ -49,7 +53,9 @@ permissionRouter.patch(
const response = await controller.updatePermission(permissionId, body)
res.send(response)
} catch (err: any) {
res.status(403).send(err.toString())
const statusCode = err.code
delete err.code
res.status(statusCode).send(err.message)
}
}
)
@@ -65,7 +71,9 @@ permissionRouter.delete(
await controller.deletePermission(permissionId)
res.status(200).send('Permission Deleted!')
} catch (err: any) {
res.status(403).send(err.toString())
const statusCode = err.code
delete err.code
res.status(statusCode).send(err.message)
}
}
)

View File

@@ -190,7 +190,7 @@ describe('permission', () => {
expect(res.body).toEqual({})
})
it('should respond with forbidden Request (403) if user is not found', async () => {
it('should respond with not found (404) if user is not found', async () => {
const res = await request(app)
.post('/SASjsApi/permission')
.auth(adminAccessToken, { type: 'bearer' })
@@ -198,13 +198,13 @@ describe('permission', () => {
...permission,
principalId: 123
})
.expect(403)
.expect(404)
expect(res.text).toEqual('Error: User not found.')
expect(res.text).toEqual('User not found.')
expect(res.body).toEqual({})
})
it('should respond with forbidden Request (403) if group is not found', async () => {
it('should respond with not found (404) if group is not found', async () => {
const res = await request(app)
.post('/SASjsApi/permission')
.auth(adminAccessToken, { type: 'bearer' })
@@ -212,13 +212,13 @@ describe('permission', () => {
...permission,
principalType: 'group'
})
.expect(403)
.expect(404)
expect(res.text).toEqual('Error: Group not found.')
expect(res.text).toEqual('Group not found.')
expect(res.body).toEqual({})
})
it('should respond with forbidden Request (403) if principal type is not valid', async () => {
it('should respond with Bad Request if principal type is not valid', async () => {
const res = await request(app)
.post('/SASjsApi/permission')
.auth(adminAccessToken, { type: 'bearer' })
@@ -226,10 +226,10 @@ describe('permission', () => {
...permission,
principalType: 'invalid'
})
.expect(403)
.expect(400)
expect(res.text).toEqual(
'Error: Invalid principal type. Valid types are user or group.'
'Invalid principal type. Valid types are user or group.'
)
expect(res.body).toEqual({})
})
@@ -295,16 +295,16 @@ describe('permission', () => {
expect(res.body).toEqual({})
})
it('should respond with forbidden Request (403) if permission with provided id does not exists', async () => {
it('should respond with not found (404) if permission with provided id does not exists', async () => {
const res = await request(app)
.patch('/SASjsApi/permission/123')
.auth(adminAccessToken, { type: 'bearer' })
.send({
setting: 'deny'
})
.expect(403)
.expect(404)
expect(res.text).toEqual('Error: Unable to update permission')
expect(res.text).toEqual('Permission not found.')
expect(res.body).toEqual({})
})
})
@@ -324,14 +324,14 @@ describe('permission', () => {
expect(res.text).toEqual('Permission Deleted!')
})
it('should respond with forbidden Request (403) if permission with provided id does not exists', async () => {
it('should respond with not found (404) if permission with provided id does not exists', async () => {
const res = await request(app)
.delete('/SASjsApi/permission/123')
.auth(adminAccessToken, { type: 'bearer' })
.send()
.expect(403)
.expect(404)
expect(res.text).toEqual('Error: Permission is not found.')
expect(res.text).toEqual('Permission not found.')
})
})