error handling - Using panics in web application -


I am writing with Go your web application I want to change the panics to replace most errors in Atncon API And then, by holding those nervousness in high-level functions, log them in and return the error page to the user.

something like:

  event handler (body function (http.ResponseWriter, * http.Request)) function (http.ResponseWriter, * http.Request) {return function (responseWriter http.ResponseWriter, request * http.Request) {postponed recoverIfPanic (responseWriter, request) body (responseWriter, request)}} recoverIfPanic function (responseWriter http.ResponseWriter, request * http.Request) {cause = recovery () that is, do these cause == null {back} // log and http error back} function PanicIf (fault error, httpStatus integer, string description) {if error = zero {panic (MyPanicStruct {err: Hey, ht tpStatus: httpStatus, Description: Description})!}}  

In your actual code

  results Hey: = SomeApi (...) PanicIf (Hey, http.StatusInternalServerError, "SomeApi")  

In 99% of cases, I can not do anything right when, e.g. The SQL server returns an unexpected error or is missing from the file file system, I want to log in to this situation and return the error to the user. So I do not see any reason that I should manually return the "indispensable" stack manually, in fact I would lose stacktrares and references and it would be more difficult to find the cause of the error.

Am I missing something such that this approach will not work well? It appears that most of the articles have been suggested against terror / retrieval, but I do not know why it looks like good old throwing mechanism in Java (and similar languages) and it is full for web applications. Works the way.

today (!) Discussed by "" on November 4, 2014

C + + exceptions, as it did three decades ago, is difficult to use safely. Any part of your call stack can explode without warning, it is no wonder that so many C ++ stores mandate that exceptions should not be made.

This refers to "" (2012, formerly Go1.0, but still valid):

Go panic Is a feature called, and if you work hard, you can think that throwing terror in the same way, but you will be wrong.
When you throw and make an exception, you are making it a collar problem.

  A new e-mail problem ();  

For example, you can throw exceptions in C ++ when you do not convert from enum to its string Or while parsing the date with string in java.
In a world connected to the Internet, where every input from the network should be considered hostile, is actually a failure to parse the string on a date? off course not.

When you panic in Go, you are getting annoyed, this is not any other problem, it is a man's game.

  panic "unimaginable")  

Terror s is always fatal for your program.
In Panic do you never believe that your caller can solve the problem So panic continues only to keep your code integrated in extraordinary circumstances, where it is not possible for people to code Is, or anyone is used.

There is an example of its simplicity and orthogonality to not include exceptions in decision making. Using many return values ​​and a simple conference, go solves the problem of telling the programmer when things go wrong and actually protects terror for extraordinary.

Other methods using

It is being said that the article mentions a real case for "terror" (,) , and adds:

Blockquote>

Get conference libraries also that when a package's internal use of terror, its external API now Also offers clear error return values ​​

So if your use of terror is strictly an internal, that can work .


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -