jquery - catching Uncaught (in promise) DOMException javascript -
i'm trying display error when user upload corrupt file or file not , image. when upload file not image uncaught (in promise) domexception: appear.
how catch error , display in div. i've tried try , catch, window.onerror doesn't when error occur.
this code.
$('.upload-result').on('click', function(ev){ if($('#upload').get(0).files.length === 0){ $('.nofile').show(); }else{ if($.inarray($('#upload').val().split('.').pop().tolowercase(), ['gif','png','jpg','jpeg']) == -1){ $('.invalid-file').show(); }else{ $uploadcrop.croppie('result',{ //error occurs here type: 'canvas', size: 'viewport' }).then(function (resp){ $.ajax({ url: "{{url('dashboard/program/imageupload')}}", type: "post", data: {"image":resp, "id":id}, success:function(data){ $('.success').show(0, function(){ settimeout(function(){ location.href = "{{route('program')}}" }, 1000); }); }, error: function (xhr, textstatus, errorthrown) { alert("fail"); } }); }); } } }); window.onerror = function() { alert("an error"); }
adding catch statement after then, keeps application safe , can handle various errors dynamically.
$uploadcrop.croppie('result', { //error occurs here type: 'canvas', size: 'viewport', }) .then(function(resp) { $.ajax({ url: "{{url('dashboard/program/imageupload')}}", type: 'post', data: { image: resp, id: id }, success: function(data) { $('.success').show(0, function() { settimeout(function() { location.href = "{{route('program')}}" }, 1000) }) }, error: function(xhr, textstatus, errorthrown) { alert('fail') }, }) }) .catch(err => /* handle error here */) the important part this:
.catch(err => /* handle error here */) if want learn more promises , why there , how use them, read this: mdn promises or medium.com: promises explained gambling
Comments
Post a Comment