Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.21 KB | None | 0 0
  1. namespace Cardone.Backoffice.ImageSearch.Controllers
  2.  
  3. open Microsoft.AspNetCore.Mvc
  4. open Microsoft.AspNetCore.Authorization
  5. open Microsoft.AspNetCore.Diagnostics
  6. open System
  7.  
  8. type ErrorViewModel (message : string) =
  9.     member __.Message with get() = message
  10.     new() = ErrorViewModel(null)
  11.  
  12. [<Route("error")>]
  13. type ErrorController() =
  14.     inherit ControllerBase()
  15.  
  16.     let getDeepestExceptionMessage (ex : Exception) =
  17.         let rec getDeepestException (ex : exn) =
  18.             match ex.InnerException with
  19.                 | null -> ex
  20.                 | _ -> getDeepestException(ex.InnerException)
  21.         getDeepestException(ex).Message
  22.    
  23.     [<AllowAnonymous>]
  24.     member this.Index() =
  25.         let ex =
  26.             match this.HttpContext.Features.Get<IExceptionHandlerFeature>() with
  27.                 | null -> None
  28.                 | value when (not << isNull) value.Error  -> Some value.Error
  29.                 | _ -> None
  30.         let errorViewModel =
  31.             ex
  32.              |> Option.map (getDeepestExceptionMessage >> ErrorViewModel)
  33.              |> Option.defaultValue (ErrorViewModel("Произошла непредвиденная ошибка"))
  34.         this.BadRequest(errorViewModel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement