-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logger as package level logger and more
Add better error handling. Remove the context struct. Use context.Context to pass db client. Remove handler options.
- Loading branch information
Showing
9 changed files
with
278 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
errBadRequest = "Bad Request" | ||
errInternalServerError = "Internal Server Error" | ||
errNotFound = "Not Found" | ||
) | ||
|
||
var env = os.Getenv("ENV") | ||
|
||
type errorResponse struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
Stack string `json:"stack,omitempty"` | ||
} | ||
|
||
func handleErr(rw http.ResponseWriter, err error, status int) { | ||
var message string | ||
|
||
switch status { | ||
case http.StatusBadRequest: | ||
message = errBadRequest | ||
|
||
case http.StatusInternalServerError: | ||
message = errInternalServerError | ||
// @TODO: The internal server error log should be sent to a different location | ||
// ideally not in the http logs. | ||
log.ErrorE(context.Background(), errInternalServerError, err) | ||
|
||
case http.StatusNotFound: | ||
message = errNotFound | ||
} | ||
|
||
sendJSON( | ||
rw, | ||
errorResponse{ | ||
Status: status, | ||
Message: message, | ||
Stack: formatError(err), | ||
}, | ||
status, | ||
) | ||
} | ||
|
||
func formatError(err error) string { | ||
if strings.ToLower(env) == "dev" || strings.ToLower(env) == "development" { | ||
return fmt.Sprintf("[DEV] %+v\n", err) | ||
} | ||
return "" | ||
} |
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,58 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFormatError(t *testing.T) { | ||
env = "prod" | ||
s := formatError(errors.New("test error")) | ||
assert.Equal(t, "", s) | ||
|
||
env = "dev" | ||
s = formatError(errors.New("test error")) | ||
lines := strings.Split(s, "\n") | ||
assert.Equal(t, "[DEV] test error", lines[0]) | ||
} | ||
|
||
func TestHandleErr(t *testing.T) { | ||
env = "dev" | ||
f := func(rw http.ResponseWriter, req *http.Request) { | ||
handleErr(rw, errors.New("test error"), http.StatusBadRequest) | ||
} | ||
req, err := http.NewRequest("GET", "/test", nil) | ||
assert.NoError(t, err) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
f(rec, req) | ||
|
||
resp := rec.Result() | ||
|
||
errResponse := errorResponse{} | ||
err = json.NewDecoder(resp.Body).Decode(&errResponse) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, http.StatusBadRequest, errResponse.Status) | ||
assert.Equal(t, "Bad Request", errResponse.Message) | ||
|
||
lines := strings.Split(errResponse.Stack, "\n") | ||
assert.Equal(t, "[DEV] test error", lines[0]) | ||
} |
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
Oops, something went wrong.