Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/fix-blob-client-token-error-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@vercel/blob": patch
---

fix(blob): include HTTP status in error when client token retrieval fails

When `upload()` fails to retrieve a client token from `handleUploadUrl`
(e.g. the route returns 401, 403, or 500), the error message now includes
the HTTP status code and status text to help with debugging.

Closes #488
19 changes: 19 additions & 0 deletions packages/blob/src/client.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ describe('client', () => {
},
);
});

it('throws with HTTP status when handleUploadUrl returns an error', async () => {
jest.spyOn(undici, 'fetch').mockImplementation(
jest.fn().mockResolvedValueOnce({
status: 403,
statusText: 'Forbidden',
ok: false,
}),
);

await expect(
upload('foo.txt', 'Test file data', {
access: 'public',
handleUploadUrl: '/api/upload',
}),
).rejects.toThrow(
'Vercel Blob: Failed to retrieve the client token: 403 Forbidden',
);
});
});

describe('multipart upload', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/blob/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,9 @@ async function retrieveClientToken(options: {
});

if (!res.ok) {
throw new BlobError('Failed to retrieve the client token');
throw new BlobError(
`Failed to retrieve the client token: ${res.status} ${res.statusText}`,
);
}

try {
Expand Down