Hi skp,
AngularJS also includes built-in $exceptionHandler service, which handles uncaught exceptions in the application.
The default implementation of $exceptionHandler service logs the exception into the browser console. You can override this service as per your requirement.
Refer below links.
https://odetocode.com/blogs/scott/archive/2014/04/21/better-error-handling-in-angularjs.aspx
https://technology.amis.nl/2014/10/06/automatic-error-handling-in-angularjs/
https://www.c-sharpcorner.com/UploadFile/cd7c2e/exceptionhandler-in-angularjs/
Check this example. Now please take its reference and correct your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="angular_1.6.8.min.js" type="text/javascript"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller("MyController", function ($scope, $http) {
Print("Welcome to ASPSnippets.com");
});
app.config(function ($provide) {
$provide.decorator("$exceptionHandler", function ($delegate, $injector) {
return function (exception, cause) {
var $http = $injector.get("$http");
$http.post("WebServiceCS.asmx/SendEmail", { message: exception.message });
$delegate(exception, cause);
};
});
});
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
</body>
</html>
WebService
C#
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public void SendEmail(string message)
{
// Send Email Code.
using (MailMessage mm = new MailMessage("sender@gmail.com", HttpContext.Current.Session["Email"].ToString()))
{
mm.Subject = "Error Details";
mm.Body = message;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(mm.From.Address, "xxxxxx");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Web
Imports System.Web.Services
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod(EnableSession:=True)> _
Public Sub SendEmail(ByVal message As String)
' Send Email Code.
Using mm As MailMessage = New MailMessage("sender@gmail.com", HttpContext.Current.Session("Email").ToString())
mm.Subject = "Error Details"
mm.Body = message
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential(mm.From.Address, "xxxxxx")
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Using
End Sub
End Class