feat(xo-server-netbox): add data field to Netbox API errors (#5842)

Fixes #5834
This commit is contained in:
Pierre Donias 2021-07-13 17:22:51 +02:00 committed by GitHub
parent d7940292d0
commit 03ec0cab1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -7,6 +7,8 @@
> Users must be able to say: “Nice enhancement, I'm eager to test it”
- [Netbox] Add information about a failed request to the error log to help better understand what happened [#5834](https://github.com/vatesfr/xen-orchestra/issues/5834) (PR [#5842](https://github.com/vatesfr/xen-orchestra/pull/5842))
### Bug fixes
> Users must be able to say: “I had this issue, happy to know it's fixed”
@ -27,3 +29,5 @@
> - major: if the change breaks compatibility
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- xo-server-netbox minor

View File

@ -97,10 +97,9 @@ class Netbox {
}
async #makeRequest(path, method, data) {
log.debug(
`${method} ${path}`,
const dataDebug =
Array.isArray(data) && data.length > 2 ? [...data.slice(0, 2), `and ${data.length - 2} others`] : data
)
log.debug(`${method} ${path}`, dataDebug)
let url = this.#endpoint + '/api' + path
const options = {
headers: { 'Content-Type': 'application/json', Authorization: `Token ${this.#token}` },
@ -116,10 +115,15 @@ class Netbox {
return JSON.parse(body)
}
} catch (error) {
error.data = {
method,
path,
body: dataDebug,
}
try {
const body = await error.response.readAll()
if (body.length > 0) {
log.error(body.toString())
error.data.error = JSON.parse(body)
}
} catch {
throw error