danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

Send error params to error handler function #1948

Open lfcgomes opened 7 years ago

lfcgomes commented 7 years ago

Hi!

I have a service which returns this

return Upload.http({
    url: requestInfo.data.url,
    method: 'POST',
    headers: headers,
    data: file
});

However, I want to catch the error that I'm receiving from the server, and parse it before being returned by the service, because the error, by itself, means nothing to the controllers that call this service.

I tried this:

return Upload.http({
    url: requestInfo.data.url,
    method: 'POST',
    headers: headers,
    data: file
}).catch(function(error){
    return $q.reject({fileID: requestInfo.data.file.id});
});

Controller:

file.upload = itemOperationService.uploadItem(uploadInfo, file);
file.upload.then(
    handleUploadSuccess, 
    handleUploadError, 
    handleUploadProgress);

It works. Now the error handler function receives this object. However, with this approach, I'm not able to call file.upload.abort() function anymore, given that it does not exist in the file.upload object . How could I achieve what I want to? Is that possible?

Thanks in advance

danialfarid commented 7 years ago

Replace your code with this:

var uploadPromise =  Upload.http({
    url: requestInfo.data.url,
    method: 'POST',
    headers: headers,
    data: file
})
uploadPromise.catch(function(error){
    return $q.reject({fileID: requestInfo.data.file.id});
});
return uploadPromise;

There are similar issues like this, only the first call would have the abort(), progress() functions registered since they are not standard promise chaining functions.

lfcgomes commented 7 years ago

@danialfarid thanks for your reply. I did what you asked my for, but doesn't work :( With that code, if the server return an error, the catch function does not catch it and the error is returned directly to the upload error handler function :\