-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recover from panic in http and grpc handlers. (#2059)
* Recover from panic in http and grpc handlers. I don't see any good reason to crash any component during a bad request. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Add alerts to the mixin for panics. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * 😡 gomod Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
- Loading branch information
1 parent
f5b9cff
commit bce4470
Showing
16 changed files
with
372 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/weaveworks/common/httpgrpc" | ||
|
||
"github.com/grafana/loki/pkg/logql" | ||
) | ||
|
||
// StatusClientClosedRequest is the status code for when a client request cancellation of an http request | ||
const StatusClientClosedRequest = 499 | ||
|
||
// WriteError write a go error with the correct status code. | ||
func WriteError(err error, w http.ResponseWriter) { | ||
switch { | ||
case err == context.Canceled: | ||
http.Error(w, err.Error(), StatusClientClosedRequest) | ||
case err == context.DeadlineExceeded: | ||
http.Error(w, err.Error(), http.StatusGatewayTimeout) | ||
case logql.IsParseError(err): | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
default: | ||
if grpcErr, ok := httpgrpc.HTTPResponseFromError(err); ok { | ||
http.Error(w, string(grpcErr.Body), int(grpcErr.Code)) | ||
return | ||
} | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} |
Oops, something went wrong.