-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06ddfe4
commit 79341e5
Showing
5 changed files
with
63 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package utils | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"net/http" | ||
) | ||
|
||
type customError struct { | ||
error | ||
StatusCode int | ||
} | ||
|
||
func ErrorHandler(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
// Add logic here to then send to jarvis with the correct values | ||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusNotFound) | ||
} | ||
}() | ||
|
||
ww := &responseWriterWrapper{ResponseWriter: w} | ||
next.ServeHTTP(ww, r) | ||
|
||
if ww.error != nil { | ||
//statusCode := http.StatusInternalServerError | ||
statusCode := http.StatusNotFound | ||
if errors.Is(ww.error, sql.ErrNoRows) { | ||
statusCode = http.StatusNotFound | ||
} else if err, ok := ww.error.(*customError); ok { | ||
statusCode = err.StatusCode | ||
} | ||
|
||
w.WriteHeader(statusCode) | ||
json.NewEncoder(w).Encode(map[string]string{"error": ww.error.Error()}) | ||
} | ||
}) | ||
} | ||
|
||
type responseWriterWrapper struct { | ||
http.ResponseWriter | ||
error error | ||
} | ||
|
||
func (w *responseWriterWrapper) WriteHeader(statusCode int) { | ||
if statusCode >= http.StatusBadRequest { | ||
w.error = fmt.Errorf("HTTP %d: %s", statusCode, http.StatusText(statusCode)) | ||
} | ||
w.ResponseWriter.WriteHeader(statusCode) | ||
} |