Custom error handling with asp.net core and suave in F#

168 Views Asked by At

This question is to validate the custom error handling implementation here or is there a better workaround possible?

Is there any other best possible way that you could suggest?

I am using Suave with Asp.Net core and kestrel server in F# to build microservices. I want to add http status code and error message for specific server side errors for the client using the microservices' restful api's.

I have the following code.

  1. Startup.fs code ==>

      member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment, loggerFactory: ILoggerFactory) = 
    
            app.UseSuaveErrorHandler(ConfigApp.ErrHandler) |> ignore 
            <TODO> 
    

  type ConfigApp () = 
    static member ErrHandler (ex:exn) (message:string) (httpContext:HttpContext) = 

    let logRepository = 
    log4net.LogManager.GetRepository(Assembly.GetEntryAssembly()) 
    log4net.Config.XmlConfigurator.Configure(logRepository, 
    FileInfo("log4net.config")) |> ignore 
    FloLogger.debug "Exception= %s" ex.Message 
    FloLogger.debug "Exception= %s" ex.StackTrace 

    match ex.Message with 
        | x -> INTERNAL_ERROR ("Custom Error Handler: " + ex.Message) httpContext 

  1. InnerMethod fs file code excerpt

   let fnGetManufacturerInfoByIdDAL (manufacturerID:string) = 
   try 
       FloLogger.debug "Inside fnGetManufacturerInfoByIdDAL method" 
       printfn "Inside fnGetManufacturerInfoByIdDAL method - %s" manufacturerID 
       use session = objSettings.OpenSession() 
       // Querying database 
       let manufacturerList = session.Load<Manufacturer>((manufacturerID)) 
       manufacturerList.Etag <- session.Advanced.GetEtagFor(manufacturerList); 
       // disposing the opened db session 
       session.Dispose() 
       manufacturerList 
  with 
      | smilie Exception as ex -> FloLogger.debug "%s" ex.Message 
                                FloLogger.debug "%s" ex.StackTrace 
                                let strException = "{\"error occurrred...\":\"error..\"}" + ex.Message 
                                failwith strException 

So the exception raised by the code in the inner method file get's handled by the Suave error handler which sends the http status code and the exception message in the response as shown in the screenshot attached. But the server errors available with Suave are very few and from them Internal_Error suits the code errors the best.

0

There are 0 best solutions below