feat(BBD 842): Improper error handling in api-javascript#81
feat(BBD 842): Improper error handling in api-javascript#81
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #81 +/- ##
===========================================
+ Coverage 95.08% 99.17% +4.08%
===========================================
Files 3 3
Lines 224 241 +17
Branches 19 21 +2
===========================================
+ Hits 213 239 +26
+ Misses 1 0 -1
+ Partials 10 2 -8
Continue to review full report at Codecov.
|
jatkinson10
left a comment
There was a problem hiding this comment.
I’m good with this. Can i get a quick demo?
| @@ -1,4 +1,5 @@ | ||
| import * as axios from 'axios'; |
|
|
||
| expect(bridge.config).to.eql({ | ||
| timeout: 4000 | ||
| }); |
| _id: id, | ||
| _u: url, | ||
| _t: title, | ||
| }; |
There was a problem hiding this comment.
Too many keys for a one line object.
| }); | ||
| bridge.search(new Query('skirts')).catch((err) => { | ||
| expect(err).to.be.an.instanceof(BridgeTimeout); | ||
| expect(err.message).to.eql('Timed out in 1500 ms'); |
There was a problem hiding this comment.
may want to comment that this is the default timeout value
|
|
||
| constructor(config: BridgeConfig) { | ||
| this.config = Object.assign({}, DEFAULT_CONFIG, config); | ||
| this.fetch = fetchPonyfill().fetch; |
There was a problem hiding this comment.
is there a reason we set this in the constructor rather than with the instance variable declaration?
class AbstractBridge {
fetch: fetchPonyfill = fetchPonyfill().fetch;
}| export abstract class AbstractBridge { | ||
|
|
||
| config: BridgeConfig; | ||
| fetch: fetchPonyfill; |
There was a problem hiding this comment.
is this the correct type?
There was a problem hiding this comment.
I'm not sure what the correct type is here, this seemed most correct to me, is there a better one?
| export class BridgeTimeout extends Error { | ||
| constructor (err: string) { | ||
| super(err); | ||
| Object.setPrototypeOf(this, BridgeTimeout.prototype); |
There was a problem hiding this comment.
Why are we calling this?
There was a problem hiding this comment.
Tests were failing before when I didn't have it. Now they're not. I guess I'll just remove it.
| }; | ||
| return axios(options) | ||
| .then((res) => res.data) | ||
| const timeoutPromise = () => |
There was a problem hiding this comment.
rather than make this a function created every time fireRequest() is called, let's make this a function at the top level called createTimeoutPromise() that accepts a timeout parameter
| }); | ||
|
|
||
| const params = qs.stringify(queryParams); | ||
| url = params ? url + '?' + params : url; |
There was a problem hiding this comment.
let's use a template literal here
`${url}?${params}`|
|
||
| const params = qs.stringify(queryParams); | ||
| url = params ? url + '?' + params : url; | ||
| const response = Promise.race([this.fetch(url, options), timeoutPromise()]) |
There was a problem hiding this comment.
I like this solution 👍 very elegant
| return res.json(); | ||
| } else { | ||
| return res.json().then((err) => { | ||
| throw { |
There was a problem hiding this comment.
We should throw another custom Error type here for clarity
| export class BridgeError extends Error { | ||
| constructor (statusText: string, public status: number, public data: any) { | ||
| super(statusText); | ||
| Object.setPrototypeOf(this, BridgeError.prototype); |
There was a problem hiding this comment.
We need this one or else the prototype is going to be the same as Error, and the getter I defined lower down won't work.
| export class BridgeError extends Error { | ||
| constructor (statusText: string, public status: number, public data: any) { | ||
| super(statusText); | ||
| Object.setPrototypeOf(this, BridgeError.prototype); |
effervescentia
left a comment
There was a problem hiding this comment.
found a couple of changes
|
|
||
| const params = qs.stringify(queryParams); | ||
| url = params ? `${url}?${params}` : url; | ||
| const response = Promise.race([this.fetch(url, options), createTimeoutPromise(this.config.timeout)]) |
There was a problem hiding this comment.
can just be returned directly
|
|
||
| export class BridgeTimeout extends Error { | ||
| constructor (err: string) { | ||
| super(err); |
There was a problem hiding this comment.
we lost overall coverage because of these lines, can we add a simple test to this
|
|
||
| export class BridgeError extends Error { | ||
| constructor (statusText: string, public status: number, public data: any) { | ||
| super(statusText); |
There was a problem hiding this comment.
we lost overall coverage because of these lines, can we add a simple test to this
| } | ||
|
|
||
| get statusText() { | ||
| return this.message; |
There was a problem hiding this comment.
we lost overall coverage because of these lines, can we add a simple test to this
There was a problem hiding this comment.
Oops forgot to commit it last time, done.
Labeling feat since we're also switching from axios to fetch-ponyfill.