Added documentation for error handling

This commit is contained in:
Alex Holliday committed 2024-05-27 11:03:12 -07:00
1 parent 950222f94e
commit 5fa73a1f17
2 files changed
+42 -4

No files matched your search

+36 -1
View File
@@ -18,7 +18,8 @@ BlueWave uptime monitoring application
- <code>POST</code> [/api/v1/monitors](#post-monitors)
- <code>POST</code> [/api/v1/monitors/delete/{monitorId}](#post-monitors-del-id)
- <code>POST</code> [/api/v1/monitors/edit/{monitorId}](#post-monitors-edit-id)
5. [Contributors](#contributors)
5. [Error Handling](#error-handling)
6. [Contributors](#contributors)
---
@@ -544,6 +545,40 @@ curl --request POST \
---
### Error handling {#error-handling}
Errors are returned in a standard format:
`{"success": false, "msg": "No token provided"}`
Errors are handled by error handling middleware and should be thrown with the following parameters
| Name | Type | Default | Notes |
| ------- | --------- | ---------------------- | ------------------------------------ |
| status | `integer` | 500 | Standard HTTP codes |
| message | `string` | "Something went wrong" | An error message |
| service | `string` | "Unknown Service" | Name of service that threw the error |
Example:
```
const myRoute = async(req, res, next) => {
try{
const result = myRiskyOperationHere();
}
catch(error){
error.status = 404
error.message = "Resource not found"
error.service = service name
next(error)
return;
}
}
```
Errors should not be handled at the controller level and should be left to the middleware to handle.
## Contributors
<a href="https://github.com/bluewave-labs/bluewave-uptime/graphs/contributors">
+6 -3
View File
@@ -1,9 +1,12 @@
const logger = require("../utils/logger");
const handleErrors = (error, req, res, next) => {
error.status = error.status ? error.status : 500;
logger.error(error.message, { service: error.service });
res.status(error.status).json({ success: false, msg: error.message });
const status = error.status || 500;
const message = error.message || "Something went wrong";
const service = error.errorService || "Unknown service";
logger.error(error.message, { service: service });
res.status(status).json({ success: false, msg: message });
};
module.exports = { handleErrors };