Skip to content

Commit 391d887

Browse files
committed
refactor: add status code in all error response
Need to standardize error response format. Chose to add the status field in all error response body. We should adopt RFC7807 [0] for error response format in the future. [0] https://tools.ietf.org/html/rfc7807
1 parent ed5fa83 commit 391d887

File tree

7 files changed

+11
-11
lines changed

7 files changed

+11
-11
lines changed

src/api/columns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ router.get('/', async (req, res) => {
2525
return res.status(200).json(payload)
2626
} catch (error) {
2727
console.log('throwing error')
28-
res.status(500).send('Database error.')
28+
res.status(500).json({ error: 'Database error.', status: 500 })
2929
}
3030
})
3131

src/api/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ router.get('/', async (req, res) => {
1111
return res.status(200).json(data)
1212
} catch (error) {
1313
console.log('throwing error')
14-
res.status(500).send('Database error.')
14+
res.status(500).json({ error: 'Database error.', status: 500 })
1515
}
1616
})
1717
router.get('/version', async (req, res) => {
@@ -20,7 +20,7 @@ router.get('/version', async (req, res) => {
2020
return res.status(200).json(data[0]) // only one row
2121
} catch (error) {
2222
console.log('throwing error')
23-
res.status(500).send('Database error.')
23+
res.status(500).json({ error: 'Database error.', status: 500 })
2424
}
2525
})
2626

src/api/policies.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ router.post('/', async (req, res) => {
4747
} catch (error) {
4848
// For this one, we always want to give back the error to the customer
4949
console.log('POST error!', error)
50-
res.status(200).json([{ error: error.toString() }])
50+
res.status(200).json([{ error: error.toString(), status: 200 }])
5151
}
5252
})
5353

@@ -84,7 +84,7 @@ router.patch('/:id', async (req, res) => {
8484
} catch (error) {
8585
// For this one, we always want to give back the error to the customer
8686
console.log('Soft error!', error)
87-
res.status(200).json([{ error: error.toString() }])
87+
res.status(200).json([{ error: error.toString(), status: 200 }])
8888
}
8989
})
9090

src/api/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ router.post('/', async (req, res) => {
1111
} catch (error) {
1212
// For this one, we always want to give back the error to the customer
1313
console.log('Soft error!', error)
14-
res.status(200).json([{ error: error.toString() }])
14+
res.status(200).json([{ error: error.toString(), status: 200 }])
1515
}
1616
})
1717

src/api/tables.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ router.get('/:id', async (req, res) => {
3636
const sql = selectSingleSql(sqlTemplates, id)
3737
const table = (await RunQuery(req.headers.pg, sql)).data[0]
3838
if (typeof table === 'undefined') {
39-
return res.status(404).json({ error: `No table exists with ID ${id}.` })
39+
return res.status(404).json({ error: `No table exists with ID ${id}.`, status: 404 })
4040
}
4141

4242
return res.status(200).json(table)
@@ -65,7 +65,7 @@ router.post('/', async (req, res) => {
6565
} catch (error) {
6666
// For this one, we always want to give back the error to the customer
6767
console.log('Soft error!', error)
68-
res.status(200).json([{ error: error.toString() }])
68+
res.status(200).json([{ error: error.toString(), status: 200 }])
6969
}
7070
})
7171

@@ -100,7 +100,7 @@ router.patch('/:id', async (req, res) => {
100100
} catch (error) {
101101
// For this one, we always want to give back the error to the customer
102102
console.log('Soft error!', error)
103-
res.status(200).json([{ error: error.toString() }])
103+
res.status(200).json([{ error: error.toString(), status: 200 }])
104104
}
105105
})
106106

src/api/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ router.get('/', async (req, res) => {
2525
return res.status(200).json(payload)
2626
} catch (error) {
2727
console.log('throwing error')
28-
res.status(500).json({ error: 'Database error', status: 500 })
28+
res.status(500).json({ error: 'Database error.', status: 500 })
2929
}
3030
})
3131

src/lib/connectionMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export const addConnectionToRequest = async (req, res, next) => {
2121
return next()
2222
} catch (error) {
2323
console.log('error', error)
24-
return res.status(500).json({ status: 500, error: 'Server error.' })
24+
return res.status(500).json({ error: 'Server error.', status: 500 })
2525
}
2626
}

0 commit comments

Comments
 (0)