/** * HTTP statuses the Git hosting service, or a proxy in front of it, answers a request with when it * has taken too long or is too large: 408 Request Timeout, 413 Content Too Large, 499 Client Closed * Request, 502 Bad Gateway and 504 Gateway Timeout. */ const UPLOAD_FAILURE_STATUSES = [408, 413, 499, 502, 504]; /** * HTTP statuses a commit request is refused with before it gets to the repository, so the commit * hasn’t been made: 401 Unauthorized, 403 Forbidden, 404 Not Found and 429 Too Many Requests. A 409 * Conflict or a 422 Unprocessable Entity isn’t among them, as GitHub and Gitea/Forgejo refuse a * repeat of a commit that has landed with one of those. */ const REFUSED_STATUSES = [401, 403, 404, 429]; /** * Check if a commit request has failed because it took too long or was too large, which the * browser typically reports as a network error, as the answer of the service, or the proxy in front * of it, comes without CORS headers. * @param {any} ex Error thrown. * @returns {boolean} Result. */ export const isUploadFailure = (ex) => ex?.message === 'Failed to send the request' || UPLOAD_FAILURE_STATUSES.includes(ex?.cause?.status); /** * Check if a commit request has been refused before it got to the repository, so the commit hasn’t * been made. Any other failure, including a server error or a response that can’t be read, may come * after the commit has been made. * @param {any} ex Error thrown. * @param {number[]} [otherStatuses] Statuses the service also refuses a request with before it gets * to the repository, e.g. 422 Unprocessable Entity on GitLab, which refuses a repeat of a commit * that has landed with 400 Bad Request instead. * @returns {boolean} Result. */ export const isRefusedBeforeCommit = (ex, otherStatuses = []) => [...REFUSED_STATUSES, ...otherStatuses].includes(ex?.cause?.status); /** * Check if a commit request has failed with `4:Deadline Exceeded`, which GitLab answers with 400 * Bad Request when Gitaly, its Git storage service, has taken too long over the commit. Unlike * other 400 errors, it doesn’t mean the commit has been refused: Gitaly may have made it before or * after the deadline, so the commit may well have landed. * @param {any} ex Error thrown. * @returns {boolean} Result. * @see https://docs.gitlab.com/user/project/repository/branches/#error-failed-to-create-branch-4deadline-exceeded * @see https://gitlab.com/gitlab-org/gitlab/-/issues/349731 * @see https://github.com/sveltia/sveltia-cms/issues/1202 */ export const isDeadlineExceeded = (ex) => ex?.cause?.status === 400 && /\bDeadline Exceeded\b/i.test(ex?.cause?.message ?? '');