Error Handling
Learn how to handle specific server errors in your EdgeStore application.
You might need to handle specific server errors in your application. Here is an example of how you can do that.
import {
EdgeStoreApiClientError,
UploadAbortedError,
UploadCanceledError,
UploadProcessingTimeoutError,
} from '@edgestore/react/errors';
// ...
<button
onClick={async () => {
try {
if (file) {
const res = await edgestore.publicFiles.upload({
file,
options: { waitForProcessing: true },
});
}
} catch (error) {
// All errors are typed and you will get intellisense for them
if (error instanceof EdgeStoreApiClientError) {
// if it fails due to the `maxSize` set in the router config
if (error.data.code === 'FILE_TOO_LARGE') {
alert(
`File too large. Max size is ${formatFileSize(
error.data.details.maxFileSize,
)}`,
);
}
// if it fails due to the `accept` set in the router config
if (error.data.code === 'MIME_TYPE_NOT_ALLOWED') {
alert(
`File type not allowed. Allowed types are ${error.data.details.allowedMimeTypes.join(
', ',
)}`,
);
}
// if it fails during the `beforeUpload` check
if (error.data.code === 'UPLOAD_NOT_ALLOWED') {
alert("You don't have permission to upload files here.");
}
} else if (error instanceof UploadAbortedError) {
// if the upload was canceled from an AbortController's signal
console.log('Upload aborted');
} else if (error instanceof UploadCanceledError) {
// if EdgeStore could not process the uploaded file
alert('This file could not be processed.');
} else if (error instanceof UploadProcessingTimeoutError) {
// if processing outlasted `waitForProcessing.timeoutMs`;
// the file may still finish processing
console.log('Still processing', error.id);
} else {
// unknown error
console.error(error);
}
}
}}
disabled={!file || !!uploadedUrl}
>
Upload
</button>Error Codes
BAD_REQUESTFILE_TOO_LARGEMIME_TYPE_NOT_ALLOWEDUNAUTHORIZEDUPLOAD_NOT_ALLOWEDDELETE_NOT_ALLOWEDCREATE_CONTEXT_ERRORSERVER_ERROR
File mutation errors
confirm and delete throw EdgeStoreFileMutationError when the provider
rejects that file. It carries the provider error code, the message, and the
fileRef it applied to. confirmMany and deleteMany do not throw for
per-file failures; inspect the returned failed list instead.
import { EdgeStoreFileMutationError } from '@edgestore/react/errors';
try {
await edgestore.publicFiles.delete({ url });
} catch (error) {
if (error instanceof EdgeStoreFileMutationError) {
console.error(error.code, error.fileRef.url);
}
}EdgeStoreClientError reports client-side failures, such as an EdgeStore API
route that does not return JSON.