Base solution for your next web application
Open Closed

Overload abp.ng.http.showError method #5123


User avatar
0
dreamsmiths created

Good day,

How can I override / overload the abp.ng.http.showError method in my angular project?

After reading couple of post, I have tried the following without success:

  1. added "../node_modules/abp-web-resources/Abp/Framework/scripts/libs/angularjs/abp.ng.js" to the .angular-cli.json file

  2. Created a js file with the following code:

var abp = abp || {};

(function () {

    abp.ng.http.showError = function (error) {
        alert("am in!!");
       
    }
})();
  1. Added js file to the .angular-cli.json file.

For testing, I have added an alert in the showError method of "\node_modules\abp-ng2-module\src\abpHttp.ts" and alert was showing for all error messages. Should this method be overwritten instead of the one in the js file? .

showError(error: IErrorInfo): any {
		alert("am in");
        if (error.details) {
            return this._messageService.error(error.details, error.message || this.defaultError.message);
        } else {
            return this._messageService.error(error.message || this.defaultError.message);
        }
    }

14 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @dreamsmiths,

    "abp.ng.http" is used for AngularJs not for Angular. So, if you could tell us what you are trying to achieve, we can make some suggestions.

  • User Avatar
    0
    dreamsmiths created

    Hi

    I did edit the post, not sure if you saw it.

    "For testing, I have added an alert in the showError method of "\node_modules\abp-ng2-module\src\abpHttp.ts" and alert was showing for all error messages. Should this method be overwritten instead of the one in the js file? .

    showError(error: IErrorInfo): any {
          alert("am in");
            if (error.details) {
                return this._messageService.error(error.details, error.message || this.defaultError.message);
            } else {
                return this._messageService.error(error.message || this.defaultError.message);
            }
        }
    

    "

    I want to catch a specific UserFriendlyException and act on it.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry, I missed that. You can't override it like that because next time when you install packages, your changes will be gone. The proper way is to define your own Angular HttpInterceptor and use it <a class="postlink" href="https://angular.io/api/common/http/HttpInterceptor">https://angular.io/api/common/http/HttpInterceptor</a>.

  • User Avatar
    0
    dreamsmiths created

    Hi,

    Yes I am aware of that, I was just placing the alert in there to see if that's the method being used. I will look into http interceptors.

    Thanks in a million.

  • User Avatar
    0
    dreamsmiths created

    Hi,

    Yes I am aware of that, I was just placing the alert in there to see if that's the method being used. I will look into http interceptors.

    Thanks in a million.

  • User Avatar
    0
    dreamsmiths created

    Hi

    I am running into some issues in using the interceptors.

    Is there any way to override/ replace the showError function with my own implementation?

    Snippet of current code:

    showError(error: IErrorInfo): any {
          alert("am in");
            if (error.details) {
                return this._messageService.error(error.details, error.message || this.defaultError.message);
            } else {
                return this._messageService.error(error.message || this.defaultError.message);
            }
        }
    

    Regards David

  • User Avatar
    0
    ismcagdas created
    Support Team

    @dreamsmiths not that I know. What is the problem ? Maybe we could help.

  • User Avatar
    0
    dreamsmiths created

    We need to upgrade a lot of packages to be able to use interceptors, including swagger etc. This is giving lots of issues, and have spent most of the day struggling with upgrades, and don't really want to take the risk in upgrading.

    The thing we want to do is:

    • Server side we currently throw a specific UserFriendlyException (this error can be thrown from multiple API calls )
    • Clientside we want to check this UserFriendlyException at a global level, and if this matches a specific String, we want to log the user out.

    There must be a way to replace/overriding the "showError" method, without changing the "\node_modules\abp-ng2-module\src\abpHttp.ts" file?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    At the end,

    abp.message.error
    

    function is used. You can replace this function with your own code and see if you can handle it like you want.

  • User Avatar
    0
    dreamsmiths created

    I don't see this in the "\node_modules\abp-ng2-module\src\abpHttp.ts" file:

    abp.message.error
    

    Where would I be able to change it, without changing the node_module files?

  • User Avatar
    0
    dreamsmiths created

    I don't see this in the "\node_modules\abp-ng2-module\src\abpHttp.ts" file:

    abp.message.error
    

    Where would I be able to change it, without changing the node_module files? Can you please show how to do this?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It is defined in node_modules\abp-web-resources\Abp\Framework\scripts\abp.js and then overriden in node_modules\abp-web-resources\Abp\Framework\scripts\libs\abp.sweet-alert.js

  • User Avatar
    0
    dreamsmiths created

    Hi

    How would you replace this function you mentioned, without changing the "\node_modules\abp-ng2-module\src\abpHttp.ts" file?

    Regards

  • User Avatar
    0
    ismcagdas created
    Support Team

    Just create a new js file and include it into angular.json (or .angular-cli.json for Angular v5) after abp.sweet-alert.js. It will work after abp.sweet-alert.js and will override the function.