Welcome to the new Box Support website. Check out all the details here on what’s changed.

Error Handling Python (ie. 409 error)

Answered
New post

Comments

1 comment

  • jcleblanc

    Hi ,

     

    Here's what you'll want to do:

    1. Listen for the 409 error with the item name in use description coming back whenever you're trying to upload a file (try / catch block) 
    2. When that error occurs, you can do a few things. You can instead call the update file endpoint to push a new file version up, or you can change the file name (e.g. filename(2).txt) and try the upload again.  

    I don't have the code in Python, but I do in Node and Java, if that helps provide some guidance:

     

    Node

    client.files.uploadFile('0', 'tempdoc.txt', stream).then((err, metadata) => {
      console.log("file uploaded");
    }).catch(function (err) {
      if (err.response && err.response.body && err.response.body.code === 'item_name_in_use') {
        console.log('duplicate file detected');
      }
    });  

    Java

    String filePath = "/localdir/temp.txt";
    String fileName = "current.txt";
    String folderId = "0";
    
    // Select Box folder
    BoxFolder folder = new BoxFolder(client, folderId);
    		
    try {
        // Upload file
        FileInputStream stream = new FileInputStream(filePath);
        folder.uploadFile(stream, fileName);
        stream.close();
    } catch (Exception e) {
        if (e.getMessage().contains("item_name_in_use")) {
            System.out.println("Duplicate file name detected");
        }
    }
    0
    Comment actions Permalink

Please sign in to leave a comment.