Skip to content

Commit d837ecb

Browse files
committed
Add httpCode to custom errors
1 parent c235a13 commit d837ecb

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file. Dates are displayed in UTC.
44

5+
### [3.2.0](https://github.com/doesdev/get-scrud/compare/3.1.0...3.2.0)
6+
7+
> 2023-09-18
8+
9+
- [Feature] Add httpCode to custom errors
10+
511
### [3.1.0](https://github.com/doesdev/get-scrud/compare/3.0.0...3.1.0)
612

713
> 2023-07-26

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ export default (opts = {}) => {
118118
const handleError = (e) => {
119119
e = e || {}
120120
const res = e.response || {}
121+
const makeError = (errIn) => {
122+
return Object.assign(new Error(errIn), { httpCode: res.status })
123+
}
121124

122-
if ((res.data || {}).error) return reject(new Error(res.data.error))
123-
if (res.status === 401) return reject(new Error('Unauthorized'))
124-
if (e.code === 'ECONNRESET') return reject(new Error('Request timeout'))
125+
if ((res.data || {}).error) return reject(makeError(res.data.error))
126+
if (res.status === 401) return reject(makeError('Unauthorized'))
127+
if (e.code === 'ECONNRESET') return reject(makeError('Request timeout'))
125128

126129
const filteredJson = (Boolean(jwt) && JSON.stringify(e)) || ''
127130

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "get-scrud",
3-
"version": "3.1.0",
3+
"version": "3.2.0",
44
"description": "Client for SCRUD style rest APIs",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)