feat(toast): add toast and error handling (MIN-283)
Error handling in thunks is handled by errorMiddleware. An error message is obtained via the getErrorMessage
function and returned in thunk via rejectWithValue
. Then, when the thunk has status isRejected
and has an error message returned in payload, this error is shown by calling the showToast
function in errorMiddleware. When thunk does not return an error message, showToast
is invoked with a generic errorMessage.
showToast
accepts 2 toast types 'success' and 'error'
and a message
parameter
The getErrorMessage
function is designed to provide a comprehensive error message based on various input scenarios. It takes in a configuration object with three parameters: error
, message
, and prefix
.
Error Handling Logic: The function first evaluates the presence of the message
parameter. If provided, it takes precedence and assigns the value directly to errorMessage
.
Axios Error Handling: If no message
is provided, it checks whether the error object is an Axios
error using axios.isAxiosError()
. Axios
errors include additional information such as status codes and response data. In such cases, the function delegates the extraction of the error message to extractAxiosErrorMessage
. The extractAxiosErrorMessage
function takes an Axios
error object and attempts to extract the error message from it. It first checks if the error response contains a reason
field within its data. If found, it returns the value of reason
.
If no reason
field is present, it retrieves the HTTP status code
from the response and maps it to a corresponding error message using HTTP_ERROR_MESSAGES
defined in the constants file. If no mapping is found, it defaults to UNKNOWN_ERROR
.
Generic Error Handling: If the error is not an Axios
error and is an instance of the standard JavaScript Error object, the function retrieves the error message directly from the error object and assigns it to errorMessage
.
Fallback: If none of the above conditions are met, it defaults to a generic error message defined as UNKNOWN_ERROR
.
Prefixing: Finally, if a prefix is provided in the configuration object, the error message is prepended with this prefix followed by a colon (:).
Axios Error status 401
JavaScript Error
User Overlay Edit Success
Add and Remove User Overlay Success