Skip to content

Commit

Permalink
in case a HTTP error happens, let us create a custom error object wit…
Browse files Browse the repository at this point in the history
…h the (error) body
  • Loading branch information
Bo Simonsen authored and druppy committed Oct 8, 2020
1 parent 161b884 commit 5b56778
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RestEntityBase, Options } from './restful'
import { rpc_sess, rpc_names_sess, fetch_smd_sess, Methods } from './rpc'
import {LocaleFn} from './common'
export {Entity, Iter, WatchCallback} from './store'
export {RestIter} from './restful'
export {RestError, RestIter} from './restful'
export {Session} from './session'

// compatility functions, to emulate old global scope
Expand Down
59 changes: 45 additions & 14 deletions src/restful.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import {Iter, Entity, WatchCallback} from './store'
import {Session} from './session'

export class RestError extends Error {
public body = ""

constructor(message, body) {
super(message)
this.name = "RestError"
this.body = body
}
}

const rest_page_size = 24

/**
Expand Down Expand Up @@ -121,7 +131,11 @@ export class RestIter<Data> implements Iter<Data> {
headers: h
}).then((response) => {
if( this.options.http_errors && !response.ok) {
reject( new Error( "HTTP error: " + response.statusText ))
response.text().then(text => {
reject( new RestError( "HTTP error: " + response.statusText, text ))
}).catch(err => {
reject( new Error( "HTTP error: " + response.statusText ))
})
return
}
let start = 0, end = 0, total = -1
Expand Down Expand Up @@ -233,12 +247,16 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
headers: this.sess.headers_get()
}).then((res) => {
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
res.text().then(text => {
reject( new RestError( "HTTP error: " + res.statusText, text ))
}).catch(err => {
reject( new Error( "HTTP error: " + res.statusText ))
})
return
}
res.json().then(jdata => {
resolve( this.normalize( jdata ))
})
}).catch( err => {
console.error( 'Restful GET error', err );
reject( err )
Expand All @@ -256,12 +274,16 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
headers: this.sess.headers_get()
}).then(res => {
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( jdata )
res.text().then(text => {
reject( new RestError( "HTTP error: " + res.statusText, text ))
}).catch(err => {
reject( new Error( "HTTP error: " + res.statusText ))
})
return
}
res.json().then(jdata => {
resolve( jdata )
})
}).catch( err => {
// console.error( 'Restful PUT error', err )
reject( err )
Expand All @@ -279,7 +301,12 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
headers: this.sess.headers_get()
}).then((res) => {
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
res.text().then(text => {
reject( new RestError( "HTTP error: " + res.statusText, text ))
}).catch(err => {
reject( new Error( "HTTP error: " + res.statusText ))
})
return
} else {
res.json().then(jdata => {
resolve( this.normalize( jdata ))
Expand All @@ -301,12 +328,16 @@ export class RestEntityBase<Data, ArgsT> implements Entity<number, Data, ArgsT>
headers: this.sess.headers_get()
}).then( res => {
if( this.options.http_errors && !res.ok) {
reject( new Error( "HTTP error: " + res.statusText ))
} else {
res.json().then(jdata => {
resolve( jdata )
res.text().then(text => {
reject( new RestError( "HTTP error: " + res.statusText, text ))
}).catch(err => {
reject( new Error( "HTTP error: " + res.statusText ))
})
return
}
res.json().then(jdata => {
resolve( jdata )
})
}).catch( err => {
// console.error( 'Restful DELETE error', err );
reject( err )
Expand Down

0 comments on commit 5b56778

Please sign in to comment.