460407 (3) [Avatar] Offline
#1
Hi,

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
//return Some httpContext

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

-------------------------------------------------------------------------------------------------------------------------------------------------------

2. 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.



Isaac Abraham (62) [Avatar] Offline
#2
Hey!

Rather than me giving your my personal opinion, and before this forum turns into a mini StackOverflow, I'd suggest you do one of two things:

1. Raise an issue on StackOverflow or even the Suave repository directly (https://github.com/SuaveIO/suave/issues) - there's a much bigger chance of getting a good answer there (possibly even from Henrik).

2. Consider using Giraffe (https://github.com/dustinmoris/Giraffe) - a relatively recent "cousin" of Suave which is designed to work exclusively on ASP .NET Core & Kestrel - and as such might be a better fit for you (whilst retaining the Suave-esque functional-first programming model).

Hope that helps!

Isaac
460407 (3) [Avatar] Offline
#3
Hello Isaac,

Appreciate your prompt response.
As advised I have posted the question to stackoverflow suave forum.
Also I would like to know about the out-of-the-box features of Giraffe that are not there in Suave?
As you have specifically mentioned to use Giraffe for asp.net core & kestrel, are there any bug fixes related to the scalability and performance optimization of the application?
Could you guide us to some blogs on Giraffe?
Also I find very less contributors on GitHub for Giraffe. Is there any benchmark available for Giraffe and Suave which can help us make a better choice?
Will keep you posted in case of any issues.
Thanks for your ongoing support as always.

Regards
Rajat
Isaac Abraham (62) [Avatar] Offline
#4
My understanding is that Giraffe is being relatively regularly (the last commits were only a couple of weeks ago). The main difference with it and Suave is that Suave is a fully-standalone webserver - so it's very lightweight, but doesn't come with the array of bells and whistles that ASP .NET comes with, which Giraffe can use.

My understanding is that Kestrel is faster than Suave but I've not done tests myself.

Either one would be a good bet, and the giraffe repo (https://github.com/dustinmoris/Giraffe) has some good links to more details (plus some reasonable documentation).