-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a type guard for
AxiosError
(#2949)
Co-authored-by: Jason Kwok <JasonHK@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Determines whether the payload is an error thrown by Axios | ||
* | ||
* @param {*} payload The value to test | ||
* @returns {boolean} True if the payload is an error thrown by Axios, otherwise false | ||
*/ | ||
module.exports = function isAxiosError(payload) { | ||
return (typeof payload === 'object') && (payload.isAxiosError === true); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var createError = require('../../../lib/core/createError'); | ||
var enhanceError = require('../../../lib/core/enhanceError'); | ||
var isAxiosError = require('../../../lib/helpers/isAxiosError'); | ||
|
||
describe('helpers::isAxiosError', function () { | ||
it('should return true if the error is created by core::createError', function () { | ||
expect(isAxiosError(createError('Boom!', { foo: 'bar' }))) | ||
.toBe(true); | ||
}); | ||
|
||
it('should return true if the error is enhanced by core::enhanceError', function () { | ||
expect(isAxiosError(enhanceError(new Error('Boom!'), { foo: 'bar' }))) | ||
.toBe(true); | ||
}); | ||
|
||
it('should return false if the error is a normal Error instance', function () { | ||
expect(isAxiosError(new Error('Boom!'))) | ||
.toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters