fn: handle client connection close errors (#1196)

This commit is contained in:
Tolga Ceylan
2018-09-05 16:56:31 -07:00
committed by GitHub
parent 246c353f9a
commit 35d04cae6d
2 changed files with 14 additions and 4 deletions

View File

@@ -18,6 +18,11 @@ var (
code: http.StatusBadRequest, code: http.StatusBadRequest,
error: errors.New("Invalid JSON"), error: errors.New("Invalid JSON"),
} }
ErrClientCancel = err{
// The special custom error code to close connection without any response
code: 444,
error: errors.New("Client cancelled context"),
}
ErrCallTimeout = err{ ErrCallTimeout = err{
code: http.StatusGatewayTimeout, code: http.StatusGatewayTimeout,
error: errors.New("Timed out"), error: errors.New("Timed out"),

View File

@@ -28,12 +28,15 @@ func simpleError(err error) *models.Error {
// TODO delete me ! // TODO delete me !
func handleV1ErrorResponse(ctx *gin.Context, err error) { func handleV1ErrorResponse(ctx *gin.Context, err error) {
log := common.Logger(ctx) log := common.Logger(ctx)
if ctx.Request.Context().Err() == context.Canceled {
log.Info("request canceled") w := ctx.Writer
if ctx.Err() == context.Canceled {
log.Info("client context cancelled")
w.WriteHeader(models.ErrClientCancel.Code())
return return
} }
w := ctx.Writer
var statuscode int var statuscode int
if e, ok := err.(models.APIError); ok { if e, ok := err.(models.APIError); ok {
if e.Code() >= 500 { if e.Code() >= 500 {
@@ -71,8 +74,10 @@ func writeV1Error(ctx context.Context, w http.ResponseWriter, statuscode int, er
// HandleErrorResponse used to handle response errors in the same way. // HandleErrorResponse used to handle response errors in the same way.
func HandleErrorResponse(ctx context.Context, w http.ResponseWriter, err error) { func HandleErrorResponse(ctx context.Context, w http.ResponseWriter, err error) {
log := common.Logger(ctx) log := common.Logger(ctx)
if ctx.Err() == context.Canceled { if ctx.Err() == context.Canceled {
log.Info("request canceled") log.Info("client context cancelled")
w.WriteHeader(models.ErrClientCancel.Code())
return return
} }