File tree Expand file tree Collapse file tree 3 files changed +39
-3
lines changed
Expand file tree Collapse file tree 3 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " wrangler " : patch
3+ ---
4+
5+ Log a more helpful error when attempting to "r2 object put" a non-existent file
Original file line number Diff line number Diff line change 1+ import { mockConsoleMethods } from "../helpers/mock-console" ;
2+ import { runInTempDir } from "../helpers/run-in-tmp" ;
3+ import { runWrangler } from "../helpers/run-wrangler" ;
4+
5+ describe ( "r2 errors" , ( ) => {
6+ mockConsoleMethods ( ) ;
7+ runInTempDir ( ) ;
8+
9+ it ( "should throw a helpful error if attempting to put a missing file" , async ( ) => {
10+ const result = runWrangler (
11+ `r2 object put bucket-object-test/missing-file.txt --file ./missing-file.txt `
12+ ) ;
13+
14+ await expect ( result ) . rejects . toThrowErrorMatchingInlineSnapshot (
15+ `[Error: The file "./missing-file.txt" does not exist.]`
16+ ) ;
17+ } ) ;
18+ } ) ;
Original file line number Diff line number Diff line change @@ -259,9 +259,22 @@ export const r2ObjectPutCommand = createCommand({
259259 let object : ReadableStream ;
260260 let objectSize : number ;
261261 if ( file ) {
262- object = await createFileReadableStream ( file ) ;
263- const stats = fs . statSync ( file ) ;
264- objectSize = stats . size ;
262+ try {
263+ const stats = fs . statSync ( file ) ;
264+ objectSize = stats . size ;
265+ object = await createFileReadableStream ( file ) ;
266+ } catch ( err ) {
267+ if ( ( err as NodeJS . ErrnoException ) . code === "ENOENT" ) {
268+ throw new UserError ( `The file "${ file } " does not exist.` ) ;
269+ }
270+ const error = new UserError (
271+ `An error occurred while trying to read the file "${ file } ": ${
272+ ( err as Error ) . message
273+ } `
274+ ) ;
275+ error . cause = err ;
276+ throw error ;
277+ }
265278 } else {
266279 const buffer = await new Promise < Buffer > ( ( resolve , reject ) => {
267280 const stdin = process . stdin ;
You can’t perform that action at this time.
0 commit comments