2022-03-01 15:50:20 +00:00
|
|
|
import { getReasonPhrase } from "http-status-codes";
|
2022-04-15 14:01:52 +00:00
|
|
|
import humanBytes from "../../lib/humanBytes";
|
2022-03-01 15:50:20 +00:00
|
|
|
|
|
|
|
export default function buildUploadErrorMessage(error) {
|
|
|
|
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
|
|
|
if (error.response) {
|
|
|
|
if (error.response.data.message) {
|
|
|
|
return `Upload failed with error: ${error.response.data.message}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const statusCode = error.response.status;
|
|
|
|
const statusText = getReasonPhrase(error.response.status);
|
|
|
|
|
|
|
|
return `Upload failed, our server received your request but failed with status code: ${statusCode} ${statusText}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The request was made but no response was received. The best we can do is detect whether browser is online.
|
|
|
|
// This will be triggered mostly if the server is offline or misconfigured and doesn't respond to valid request.
|
|
|
|
if (error.request) {
|
|
|
|
if (!navigator.onLine) {
|
|
|
|
return "You are offline, please connect to the internet and try again";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should add a note "our team has been notified" and have some kind of notification with this error.
|
|
|
|
return "Server failed to respond to your request, please try again later.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match the error message to a message returned by TUS when upload exceeds max file size
|
|
|
|
const matchTusMaxFileSizeError = error.message.match(/upload exceeds maximum size: \d+ > (?<limit>\d+)/);
|
|
|
|
|
|
|
|
if (matchTusMaxFileSizeError) {
|
2022-04-15 14:01:52 +00:00
|
|
|
return `File exceeds size limit of ${humanBytes(matchTusMaxFileSizeError.groups.limit, { precision: 0 })}`;
|
2022-03-01 15:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should add a note "our team has been notified" and have some kind of notification with this error.
|
|
|
|
return `Critical error, please refresh the application and try again. ${error.message}`;
|
|
|
|
}
|