Asked by:
Global exception handling

Question
-
User582566331 posted
How to handle unhandled exceptions in web api? ie,It should direct to common error page.So from server to client side(react js)we need to pass common error message as unexpected error occured.Please guide.
Monday, February 10, 2020 6:41 AM
All replies
-
User1120430333 posted
How to handle unhandled exceptions in web api? ie,It should direct to common error page.So from server to client side(react js)we need to pass common error message as unexpected error occured.Please guide.
On the WebAPI you can do what is in the link..
https://stackify.com/csharp-catch-all-exceptions/
I suggest that you log the error and log the stacktrace and inner.exception.message too if inner.exception.message is available.
If you do GEH, then you don't need a try/catch anywhere in the WebAPI side code or a DLL the WebAPI has reference to.
I assume you are using some form of Ajax so you should be able to do Ajax exception handling on the client-side. .
This is non Core WebAPI code example using information in above link. It's straight forward code that you can use, but it's in VB.
Imports System.Net Imports System.Net.Http Imports System.Web.Http.Filters Imports log4net Public Class CustomExceptionFilter Inherits ExceptionFilterAttribute private ReadOnly _logger As ILog public sub New() 'constructor _logger = LogManager.GetLogger(GetType(CustomExceptionFilter)) End sub Public Overrides Sub OnException(actionExecutedContext As HttpActionExecutedContext) MyBase.OnException(actionExecutedContext) Dim exceptionMessage as String = String.Empty If IsNothing(actionExecutedContext.Exception.InnerException ) Then exceptionMessage = actionExecutedContext.Exception.Message _ & " " & actionExecutedContext.Exception.StackTrace Else exceptionMessage = actionExecutedContext.Exception.Message _ & " " & actionExecutedContext.Exception.InnerException.Message _ & " " & actionExecutedContext.Exception.StackTrace End If _logger.Error(exceptionMessage) dim response = New HttpResponseMessage(HttpStatusCode.InternalServerError)With {.Content = New StringContent(“An unhandled exception was thrown by service.”), .ReasonPhrase = "Internal Server Error.Please Contact your Administrator."} actionExecutedContext.Response = response End Sub End Class
Imports System.Web.Http Imports DAL Imports Entities.Entities Namespace Controllers <CustomExceptionFilter> Public Class CacheController Inherits ApiController Private ReadOnly _daocache As IDaoCache public sub New (daocache As IDaoCache) _daocache = daocache End sub <HttpGet> <ActionName("GetCache")> public Function GetCache() as CacheResponse dim resp = new CacheResponse() dim cache = _daoCache.GetCache() resp.DtoCache.Durations = cache.Durations resp.DtoCache.ProjectTypes = cache.ProjectTypes resp.DtoCache.Resources = cache.Resources resp.DtoCache.Statuses = cache.Statuses return resp End Function End Class End Namespace
.
Monday, February 10, 2020 8:18 AM -
User-474980206 posted
How to handle unhandled exceptions in web api? ie,It should direct to common error page.So from server to client side(react js)we need to pass common error message as unexpected error occured.Please guide.
in general you ajax calls are expecting json, redirecting to a error page makes little sense. your webapi should return a 500 error. you can set the 500 status message to your error text. this makes the client coding simple:
try { var response = await fetch(...) .then(response => { if (!response.ok) { throw Error(response.statusText); } return response.json(); }); // process response } catch(err) { alert(err.message); } // or define json handler: const handleJson = response => { if (!response.ok) { throw Error(response.statusText); } return response.json(); }; try { var response = await fetch(...).then(handleJson); // process response } catch(err) { alert(err.message); }
Monday, February 10, 2020 7:02 PM