Skip to content

Commit 518a624

Browse files
authored
Merge pull request #19 from codeclown/docs-update
Docs about responses and errors
2 parents 35d451c + bce1d4a commit 518a624

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

API.md

+56
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The following methods are available.
1111
- [`.send([body])`](#send) [`.sendUrlencoded(data)`](#sendurlencoded) [`.sendJson(data)`](#sendjson) [`.then()`](#then)
1212
- [`.setResponseTransformers([])`](#setresponsetransformers) [`.setAllowedStatusCode(allowed)`](#setallowedstatuscode) [`.polyfills(polyfills)`](#polyfills)
1313
- [`.toObject()` / `.config()` / `.debug()`](#toobject)
14+
- [`YeaResponse`](#yearesponse)
15+
- [`YeaRequestError`](#yearequesterror)
1416

1517
## get
1618

@@ -437,3 +439,57 @@ const config = req.toObject(); // or req.config() or req.debug()
437439
// ],
438440
// }
439441
```
442+
443+
## YeaResponse
444+
445+
Responses have the following format:
446+
447+
```js
448+
{
449+
headers: {
450+
'content-type': 'text/html',
451+
...
452+
},
453+
body: 'raw body text',
454+
status: 200
455+
}
456+
```
457+
458+
By default JSON responses are decoded (see [`.setResponseTransformers([])`](#setresponsetransformers)), in which case there is an extra property:
459+
460+
```js
461+
{
462+
headers: {
463+
'content-type': 'application/json',
464+
...
465+
},
466+
body: '{"foo":"bar"}',
467+
status: 200
468+
data: {
469+
foo: 'bar'
470+
}
471+
}
472+
```
473+
474+
## YeaRequestError
475+
476+
If a request fails, the Promise is rejected with an error which includes an extra `response` property:
477+
478+
```js
479+
{
480+
message: 'Request failed with status 500',
481+
stack: '...',
482+
// ...
483+
response: {
484+
headers: { ... },
485+
status: 500,
486+
body: '...'
487+
}
488+
}
489+
```
490+
491+
Note that yea throws regular synchronous errors if a request config is invalid, e.g.
492+
493+
```js
494+
throw new Error('Invalid header value for header \'' + name + '\'');
495+
```

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ request
9090
console.log(response.body);
9191
})
9292
.catch(error => {
93-
console.error(error.response.status);
93+
if (error.response) {
94+
console.error(error.response.status);
95+
} else {
96+
// ...
97+
}
9498
})
9599

96100
// POST requests

0 commit comments

Comments
 (0)