From 11aa9f0a0f53fd76ecacff82835c8d311db23fbb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 28 Mar 2021 19:05:43 +0200 Subject: [PATCH] fix: global handler recoverer --- handler.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/handler.go b/handler.go index 9d7f30b..e85fe49 100644 --- a/handler.go +++ b/handler.go @@ -1,6 +1,7 @@ package aicra import ( + "log" "net/http" "git.xdrm.io/go/aicra/api" @@ -13,6 +14,13 @@ type Handler Builder // ServeHTTP implements http.Handler func (s Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + defer func() { + if rc := recover(); rc != nil { + log.Printf("recovering request: %s\n", rc) + // try to send error response + api.EmptyResponse().WithError(api.ErrUncallableService).ServeHTTP(w, r) + } + }() defer r.Body.Close() // 1. find a matching service from config