-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract generic function to parse requests errors
- Loading branch information
Showing
2 changed files
with
46 additions
and
25 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,44 @@ | ||
// Copyright (c) Cosmo Tech. | ||
// Licensed under the MIT license. | ||
|
||
const _isString = (value) => { | ||
return typeof value === 'string' || value instanceof String; | ||
}; | ||
|
||
export const parseError = (error) => { | ||
if (error?.response?.data) { | ||
const errorData = error.response.data; | ||
if (_isString(errorData)) { | ||
return { | ||
title: error.response.statusText, | ||
status: error.response.status, | ||
detail: errorData, | ||
}; | ||
} | ||
return { | ||
title: errorData.title || errorData.statusText || error.response.statusText, | ||
status: errorData.status || errorData.code || error.response.status, | ||
detail: errorData.detail || errorData.message, | ||
}; | ||
} else if (error?.message === 'Network Error' || error?.title === 'Network Error') { | ||
return { | ||
title: 'Network error', | ||
status: null, | ||
detail: | ||
'No response received, please check your network settings. If the problem persists, please contact ' + | ||
'your administrator.', | ||
}; | ||
} else if (error?.request) { | ||
return { | ||
title: error.request.statusText, | ||
status: error.request.status, | ||
detail: error.request.response, | ||
}; | ||
} | ||
|
||
return { | ||
title: 'Unknown error', | ||
status: null, | ||
detail: 'An unknown error happened. If the problem persists, please contact your administrator.', | ||
}; | ||
}; |