fix(storage-azure): return error status 404 when file is not found instead of 500 (#11734)

### What?

The azure storage adapter returns a 500 internal server error when a
file is not found.
It's expected that it will return 404 when a file is not found.

### Why?

There is no checking if the blockBlobClient exists before it's used, so
it throws a RestError when used and the blob does not exist.

### How?

Check if exception thrown is of type RestError and have a 404 error from
the Azure API and return a 404 in that case.

An alternative way would be to call the exists() method on the
blockBlobClient, but that will be one more API call for blobs that does
exist. So I chose to check the exception instead.

Also added integration tests for azure storage in the same manner as s3,
as it was missing for azure storage.
This commit is contained in:
Anders Semb Hermansen
2025-06-11 16:49:34 +02:00
committed by GitHub
parent 37afbe6c04
commit 018317dfba
5 changed files with 213 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ import type { ContainerClient } from '@azure/storage-blob'
import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types'
import type { CollectionConfig } from 'payload'
import { RestError } from '@azure/storage-blob'
import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities'
import path from 'path'
@@ -66,6 +67,9 @@ export const getHandler = ({ collection, getStorageClient }: Args): StaticHandle
status: response.status,
})
} catch (err: unknown) {
if (err instanceof RestError && err.statusCode === 404) {
return new Response(null, { status: 404, statusText: 'Not Found' })
}
req.payload.logger.error(err)
return new Response('Internal Server Error', { status: 500 })
}