-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using API error messages in the interface (#149)
* show messages from ApiError * format
- Loading branch information
Showing
4 changed files
with
44 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Get error message from error object | ||
* @param {Error} e - error object | ||
* @param {?Object} [options] - options: {defaultMessage: string, withApiKey: {apiKey: string}} | ||
* @param {?string} [options.fallbackMessage] - fallback error message | ||
* @param {?Object} [options.withApiKey] - object with apiKey | ||
* @param {?string} [options.withApiKey.apiKey] - apiKey | ||
* @return {null|string} | ||
*/ | ||
export const getErrorMessage = (e, options = {}) => { | ||
const { fallbackMessage = 'Something went wrong.', withApiKey = null } = options; | ||
const { apiKey } = withApiKey || {}; | ||
let message; | ||
|
||
try { | ||
// error is instance of ApiError | ||
const error = e.getActualType(); | ||
if ((error.status === 401 || error.status === 403) && withApiKey) { | ||
if (!apiKey) { | ||
return null; | ||
} else { | ||
return 'Your API key is invalid. Please, set a new one.'; | ||
} | ||
} | ||
message = error.data?.status?.error || e.message || fallbackMessage; | ||
} catch (err) { | ||
// error is not instance of ApiError | ||
message = e.message || fallbackMessage; | ||
} | ||
return message; | ||
}; |
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