Appropriate http responses on exception in REST API -
In my module, I would like to be able to throw exceptions when I have "bad things" and this setting is automatically used with appropriate exceptions (Including the status codes and payloads, if applicable) change those exceptions. I know that I can do this from my BootStrapper on the pipeline at the Honor ceremony. But is there a better or more established way of doing this?
What I have now here:
pipelines.OnError + = (ctx, err) = & gt; Handle exception (mistake, CTX);
More method:
Fixed response handles exception (exception error, Nancy Cotex CTX) {If (not fault is availableHandler exception) {Return ctx.Response. WStringContents ("No handler exists for given command"). With StatusCode (HttpStatusCode.NotImplemented); } If (incorrect UserAlreadyExistsException) {return ctx.Response .WithStringContents (err.Message) .WithStatusCode (HttpStatusCode.BadRequest); } If there is no mistake (unimplemented exception) {return ctx.Response .WithStringContents (err.Message) .WithStatusCode (HttpStatusCode.NotImplemented); } If (Unauthorized Access Access) {return ctx.Response .WithStatusCode (HttpStatusCode.Unauthorized); } If (accidental tocdiosexist exception) {return ctx.Response. With string contracts ("The token you tried to use is not valid.") .aththascasc (HTTTPTAS code. Unauthorized); } If (tokenxoxide exception of mistake) {return ctx.Response .WithStringContents ("The token you tried to use has expired. Please log in to get a new one.") .StatusCode (HttpStatusCode .Unauthorized); } String exceptionText = AddException (mistake); Return new response () Request for string (format "{1} '{}} resulted in an unrestricted exception! \ R \ n \ r \ n {2}", ctx.Request.Method, ctx .equest .Url, exception text)) .atatcodes (HTTP.txt.CO.O. International); }
Like I say, it works. But something like this seems to be that I'm searching the wheel again and I have not found the right blog site to set it straight.
Comments
Post a Comment