Browse Source

simplify asPromise code

http2
Vsevolod Strukchinsky 9 years ago
parent
commit
2989756030
  1. 15
      index.js

15
index.js

@ -94,34 +94,27 @@ function asPromise(opts) {
const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts); const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
stream stream
.catch(err => reject(new got.ReadError(err, opts)))
.then(data => { .then(data => {
let err;
const statusCode = res.statusCode; const statusCode = res.statusCode;
res.body = data; res.body = data;
if (statusCode < 200 || statusCode > 299) {
err = new got.HTTPError(statusCode, opts);
}
if (opts.json && statusCode !== 204) { if (opts.json && statusCode !== 204) {
try { try {
res.body = JSON.parse(res.body); res.body = JSON.parse(res.body);
} catch (e) { } catch (e) {
err = new got.ParseError(e, opts, data); throw new got.ParseError(e, opts, data);
} }
} }
if (err) { if (statusCode < 200 || statusCode > 299) {
Object.defineProperty(err, 'response', {value: res}); throw new got.HTTPError(statusCode, opts);
reject(err);
return;
} }
resolve(res); resolve(res);
}) })
.catch(err => { .catch(err => {
err = new got.ReadError(err, opts);
Object.defineProperty(err, 'response', {value: res}); Object.defineProperty(err, 'response', {value: res});
reject(err); reject(err);
}); });

Loading…
Cancel
Save