Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.23 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 getExceptionMessage (ex : Exception) =
  17.         let rec getException (ex : exn) =
  18.             match ex.InnerException with
  19.                 | null -> ex
  20.                 | _ -> getException(ex.InnerException)
  21.         getException(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 ->
  29.                     match value.Error with
  30.                         | null -> None
  31.                         | value -> Some(value)
  32.         let errorViewModel =
  33.             ex
  34.              |> Option.map (getExceptionMessage >> ErrorViewModel)
  35.              |> Option.defaultValue (ErrorViewModel("Произошла непредвиденная ошибка"))
  36.         this.BadRequest(errorViewModel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement