@@ -4,7 +4,7 @@ import type { IncomingMessage } from 'node:http';
44// When bundled via a bundler supporting the `browser` field, then
55// the `undici` module will be replaced with https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
66// for browser contexts. See ./undici-browser.js and ./package.json
7- import { fetch } from 'undici' ;
7+ import { fetch , type Response } from 'undici' ;
88import type { BlobCommandOptions } from './helpers' ;
99import { BlobError , getTokenFromOptionsOrEnv } from './helpers' ;
1010import { createPutMethod } from './put' ;
@@ -329,9 +329,12 @@ async function retrieveClientToken(options: {
329329 method : 'POST' ,
330330 body : JSON . stringify ( event ) ,
331331 } ) ;
332+
332333 if ( ! res . ok ) {
333- throw new BlobError ( 'Failed to retrieve the client token' ) ;
334+ const errorMessage = await tryGetErrorMessageFromResponse ( res ) ;
335+ throw new BlobError ( errorMessage ?? 'Failed to retrieve the client token' ) ;
334336 }
337+
335338 try {
336339 const { clientToken } = ( await res . json ( ) ) as { clientToken : string } ;
337340 return clientToken ;
@@ -340,6 +343,15 @@ async function retrieveClientToken(options: {
340343 }
341344}
342345
346+ async function tryGetErrorMessageFromResponse ( res : Response ) {
347+ const jsonBody = await res . json ( ) . catch ( ( ) => null ) ;
348+ if ( ! jsonBody ) return ;
349+ if ( typeof jsonBody !== 'object' ) return ;
350+ if ( 'error' in jsonBody && typeof jsonBody . error === 'string' ) {
351+ return jsonBody . error ;
352+ }
353+ }
354+
343355function toAbsoluteUrl ( url : string ) : string {
344356 return new URL ( url , window . location . href ) . href ;
345357}
0 commit comments