Saturday, January 5, 2019

How can I message exceptions from .NET Core REST endpoints which return void?

I personally prefer a purist approach to REST in which PUTs and DELETEs are fire-and-forget returning nothing. There are some challenges with this in an Angular 6 app insofar as if you want to do a .subscribe against an Observable you cannot as the ear you are listening with never gets fed and indeed it (probably) never should. (Think exception for the exception.) I eventually learned the way around this was to just use a promise instead of an Observable for REST endpoints which return void. The second function in a .then will catch an error message in these circumstances, but getting an error message to come back up when you are specifically returning nothing takes a bit of work. I don't recommend that you return a ceremonial object which maybe has some metadata on it or even an HTTP response from the endpoint. Instead write a filter like so:

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Rest;
namespace GroverClevelandHopscotch.RestApi.Filters
{
   public class BubbleUpExceptions : ExceptionFilterAttribute
   {
      public override void OnException(ExceptionContext context)
      {
         context.Result = new JsonResult(context.Exception);
         if (context.Exception is ValidationException)
         {
            context.HttpContext.Response.StatusCode = (int)
                  HttpStatusCode.NotAcceptable;
         }
         else
         {
            context.HttpContext.Response.StatusCode = (int)
                  HttpStatusCode.InternalServerError;
         }
      }
   }
}

 
 

At Startup.cs the .AddMvc() implementation is going to have to get this filter jammed into it. It can be more verbose than what I have above, make massages as needed for whatever variants of classes inheriting from Exception as you deem fit and then make an insertion like so:

.AddMvc(options =>
   {
      options.Filters.Add(new BubbleUpExceptions());
   })

 
 

Back at the TypeScript in the Angular app do something like this:

deletePresident():void {
   this.http.delete(this.route).toPromise().then(function(){
      console.log('success');
   }, function(error){
      console.log("Error Code: " + error.status);
      console.log(JSON.parse(error._body).Message);
   });
}

 
 

this.http above comes from...

import { Http } from '@angular/http';

No comments:

Post a Comment