-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Improve the HTTP API implementation (#382)
Resolves #372 Resolves #367 This PR restructures the api/http package with idiomatic patterns and a few useful utility functions.
- Loading branch information
Showing
22 changed files
with
1,271 additions
and
309 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 was deleted.
Oops, something went wrong.
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,51 @@ | ||
// 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" | ||
) | ||
|
||
var env = os.Getenv("DEFRA_ENV") | ||
|
||
type errorResponse struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
Stack string `json:"stack,omitempty"` | ||
} | ||
|
||
func handleErr(ctx context.Context, rw http.ResponseWriter, err error, status int) { | ||
if status == http.StatusInternalServerError { | ||
log.ErrorE(ctx, http.StatusText(status), err) | ||
} | ||
|
||
sendJSON( | ||
ctx, | ||
rw, | ||
errorResponse{ | ||
Status: status, | ||
Message: http.StatusText(status), | ||
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 "" | ||
} |
Oops, something went wrong.