Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to have access to UploadApiErrorResponse to handle cloudinary errors? #449

Closed
manoellribeiro opened this issue Sep 10, 2020 · 8 comments

Comments

@manoellribeiro
Copy link

I'm trying to handle errors from the upload method, so I want to have access to the interface UploadApiErrorResponse to check if the thrown errors is from cloudinary or not. Is there a way I can do that?

For example, with Sequelize you can do:

catch (error){
   if(error instanceof Sequelize.BaseError){
    //some code
  }
}
@leptians
Copy link
Contributor

Hi @manoellribeiro , for any error in upload, it will be pass in the error param of the callback:

cloudinary.uploader.upload("my_picture.jpg", function(error, result) {
    console.log(result);
    console.error(error);
});

@manoellribeiro
Copy link
Author

Hi @leptians, thanks for your answer. I read about that in the docs, but I think it throws that error object as well, as I'm using it inside a try...catch block I would like to handle in the catch block not in the callback function.

try {
  //some code

  cloudinary.uploader.upload("my_picture.jpg");

} catch (error){

  //handle cloudinary errors and others errors
}  

@leptians
Copy link
Contributor

leptians commented Sep 10, 2020

@manoellribeiro
If you wish to use try-catch-throw method, you can implement the following:

cloudinary.uploader.upload("my_picture.jpg", function(error, result) {
    if (typeof error !== 'undefined') {
        throw .... // Throw the type of error and message of your choice.
    }
});

@manoellribeiro
Copy link
Author

@leptians
Thanks again for your help. I tried this way, but it didn't work because the app crashes if I throw anything in the callback function, I have searched for an explanation and I think this answer by Daniel Vassallo makes sense.

@leptians
Copy link
Contributor

@manoellribeiro , thanks for sharing the link and that makes sense.
The reason the error is part of the callback, because the upload is an async operation. If you are trying to try-catch-throw, that means it's expecting a synchronous call, which generally considered as bad practice within javascript world.
Perhaps, can you elaborate your use-case and why you would prefer to use this approach (sync) versus callback (async)?

@manoellribeiro
Copy link
Author

@leptians
I'm expecting it to be asynchronous as it is inside an async function:

async function(req, res, next){ //I'm using express
try {

 //asynchronous code not related to cloudinary

 const uploadResponse = await cloudinary.uploader.upload( 
            'my_picture.png',
            {
                folder: "folder/path",
                use_filename: true
            });
} catch (error){
 //handle errors
  next(error)
}
}

I don't have much experience with Javascript, so maybe I'm not using best practices as you said (I'm open for tips). But I was only trying to check the error object type that is thrown when the upload fails.

@leptians
Copy link
Contributor

Hi @manoellribeiro ,

the parent function where you have Cloudinary uploader is async, however cloudinary.uploader.upload itself is also an async operation as well, hence the callback. This uploader will return a Promise

I would still recommend to handle your error inside the callback, and this is common practice for an async operation:

cloudinary.uploader.upload("my_picture.jpg", function(error, result) {
    if (typeof error !== 'undefined') {
        // handle errors
    }
});

if you can share more detail on this async function(req, res, next){ //I'm using express and how this async function is called, perhaps I can take a look and make some recommendation on how to approach this.

If you don't feel comfortable sharing the code publicly, you can open a ticket with this detail via our support portal (https://support.cloudinary.com/hc/en-us/requests/new) and we can continue from there.

@manoellribeiro
Copy link
Author

@leptians Thank you for the help, but I have already found a way to handle it, so I think it won't be needed and I'm closing this issue as it seems to be a duplicated of #131.

I don't know if it is a best practice or not, but I did this way, if someone else come here with the same doubt:

//isCloudinaryError.js file
module.exports = function isCloudinaryError(error){
    return error.hasOwnProperty('message') && error.hasOwnProperty('name') && error.hasOwnProperty('http_code')
}

async function(req, res, next){ //I'm using express
try {

 //asynchronous code not related to cloudinary

 const uploadResponse = await cloudinary.uploader.upload( 
            'my_picture.png',
            {
                folder: "folder/path",
                use_filename: true
            });
} catch (error){
 if(isCloudinaryError(error)){
  next(new CustomErrorHandler(400, "Error uploading image")) //I send the error to my errorMiddleware handle it properly
  }
 }
}

As @leptians mentioned I could also do that:

cloudinary.uploader.upload("my_picture.jpg", function(error, result) {
    if (typeof error !== 'undefined') {
        return res.status(400).json({
        statusCode: 400,
        message: "Error uploading image"
        })
    }
});

As I'm trying to keep a pattern in my application I used the first way, but the second worked just fine as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants