appwrite/sdk-for-node

πŸ› Bug Report: Uncaught Exception on Storage#createFile

jortuck opened this issue Β· 3 comments

πŸ‘Ÿ Reproduction steps

When trying to upload a file using a bad API key, an uncaught exception is thrown.

client.setEndpoint(endpoint)
client.setProjectprojectid)
client.setKey(badkey)
const storage = new sdk.Storage(client);
try {
    const appFile = await storage.createFile("files",file.name,InputFile.fromPath(directory,file.name))
    return true;
}catch (e){
     console.log(e);
     return false;
}

πŸ‘ Expected behavior

I should be able to catch the exception my self in my own try/catch block.

try {
    const appFile = await storage.createFile("files",file.name,InputFile.fromPath(directory,file.name))
    return true;
}catch (e){
     console.log(e);
     return false;
}

πŸ‘Ž Actual Behavior

It actually crashes the whole application, and spits out a stack trace.

C:\laragon\www\myfiles\node_modules\.pnpm\node-appwrite@8.1.0\node_modules\node-appwrite\lib\client.js:171
                        throw new AppwriteException(error.response.data.message, error.response.status, error.response.data.type, error.response.data);
                              ^

AppwriteException [Error]: The current user is not authorized to perform the requested action.
    at Client.call (C:\laragon\www\myfiles\node_modules\.pnpm\node-appwrite@8.1.0\node_modules\node-appwrite\lib\client.js:171:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async uploadChunk (C:\laragon\www\myfiles\node_modules\.pnpm\node-appwrite@8.1.0\node_modules\node-appwrite\lib\services\storage.js:371:24)
    at async Writable.<anonymous> (C:\laragon\www\myfiles\node_modules\.pnpm\node-appwrite@8.1.0\node_modules\node-appwrite\lib\services\storage.js:433:21) {
  code: 401,
  type: 'user_unauthorized',
  response: {
    message: 'The current user is not authorized to perform the requested action.',
    code: 401,
    type: 'user_unauthorized',
    version: '1.1.1'
  }
}

Node.js v17.2.0
ELIFECYCLE  Command failed with exit code 1.

 

🎲 Appwrite version

Version 1.1.1

πŸ’» Operating system

Windows

🧱 Your Environment

N/A

πŸ‘€ Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏒 Have you read the Code of Conduct?

Looks like we may have assumed this would catch any errors:

writeStream.on("error", (err) => {
reject(err);
});

I think the solution might be to add a try/catch here:

writeStream.on("finish", async () => {
if(currentChunkSize > 0) {
await uploadChunk(true);
}
resolve(response);
});

like:

            writeStream.on("finish", async () => {
                if(currentChunkSize > 0) {
                    try {
                        await uploadChunk(true);
                    } catch (e) {
                        reject(e);
                    }
                }
                
                resolve(response);
            });

@DriedSponge can we actually close this as a duplicate of #45 and you can πŸ‘πŸΌ the previous issue?

@DriedSponge can we actually close this as a duplicate of #45 and you can πŸ‘πŸΌ the previous issue?

Alright I'll go ahead and do that. Thanks!