Skip to content

Commit

Permalink
Use HTTP 500 instead of HTTP 400 for generic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Feb 8, 2024
1 parent 00c60d4 commit 8584d7a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Disable "eco" mode switch on older units (https://github.com/Jalle19/eda-modbus-bridge/issues/104)
* Add optional sensors for control panel temperature, supply fan speed and exhaust fan speed (https://github.com/Jalle19/eda-modbus-bridge/issues/96)
* Remove references to "flags", use "modes" everywhere instead
* Use HTTP 500 instead of HTTP 400 for generic errors

## 2.7.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ The response is identical to that of `GET /mode/{mode}`.

### POST /setting/{setting}/{value}

Changes the setting to the specified value. HTTP 400 is thrown if the value specified is out of range or invalid.
Changes the setting to the specified value. HTTP 400 is returned if the value specified is out of range or invalid.

Returns the new setting values, like this:

Expand Down
11 changes: 8 additions & 3 deletions app/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const setSetting = async (modbusClient, req, res) => {
'settings': await getSettings(modbusClient),
})
} catch (e) {
handleError(e, res)
if (e instanceof RangeError) {
handleError(e, res, 400)
} else {
handleError(e, res)
}
}
}

Expand All @@ -100,8 +104,9 @@ export const configureRoutes = (httpServer, modbusClient) => {
})
}

const handleError = (e, res) => {
const handleError = (e, res, statusCode = undefined) => {
logger.error(`An exception occurred: ${e.name}: ${e.message}`, e.stack)
res.status(400)
// Use HTTP 500 if no status code has been set
res.status(statusCode ?? 500)
res.json(e)
}

0 comments on commit 8584d7a

Please sign in to comment.