Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the error body is not a buffer; release v0.5.12 #48

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.js
Expand Up @@ -212,12 +212,25 @@ class Request {
}

if (res.status >= 400) {
body = body || {};
if (Buffer.isBuffer(body) || body.constructor === String) {
body = body.toString();
}
if (typeof body === 'string') {
// try to parse it
try {
body = JSON.parse(body);
} catch (e) {
// not valid JSON, just include it as-is
body = { detail: body };
d00rman marked this conversation as resolved.
Show resolved Hide resolved
}
}
throw new HTTPError({
status: res.status,
headers: Object.assign({
'content-type': 'application/problem+json'
}, res.headers || {}),
body: Object.assign(res.body, {
body: Object.assign(body, {
internalURI: this.options.uri.toString(),
internalMethod: this.options.method
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "preq",
"version": "0.5.11",
"version": "0.5.12",
"description": "Yet another promising request wrapper",
"main": "index.js",
"scripts": {
Expand Down