better logging
This commit is contained in:
parent
4855255373
commit
65326b0ff5
@ -44,7 +44,7 @@ func (api *Api) Authenticate(next http.Handler) http.Handler {
|
||||
case errors.Is(err, data.ErrRecordNotFound):
|
||||
api.errorResponse(w, r, data.ErrInvalidAuthToken)
|
||||
default:
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -55,7 +55,7 @@ func (api *Api) Authenticate(next http.Handler) http.Handler {
|
||||
case errors.Is(err, data.ErrRecordNotFound):
|
||||
api.errorResponse(w, r, data.ErrInvalidCredentials)
|
||||
default:
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func (api *Api) RegisterDeviceToken(w http.ResponseWriter, r *http.Request) {
|
||||
api.App.Logger.PrintError(err, map[string]string{
|
||||
"user_id": fmt.Sprint(user.ID),
|
||||
})
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ func (api *Api) DeleteDeviceToken(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
tokens, err := api.App.Models.DeviceTokens.GetForUser(user.ID)
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
owns := false
|
||||
@ -81,7 +81,7 @@ func (api *Api) DeleteDeviceToken(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := api.App.Models.DeviceTokens.Delete(input.Token); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -16,25 +16,20 @@ func (api *Api) LogError(r *http.Request, err error) {
|
||||
}
|
||||
|
||||
func (api *Api) errorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||
apiErr := &data.Error{
|
||||
HttpCode: http.StatusInternalServerError,
|
||||
Message: "the server encountered a problem",
|
||||
}
|
||||
api.App.LogError(r, err)
|
||||
|
||||
// Try to "unbox" the error to see if it's our rich *Error type
|
||||
var customErr *data.Error
|
||||
if errors.As(err, &customErr) {
|
||||
apiErr = customErr
|
||||
}
|
||||
apiErr := &data.Error{
|
||||
HttpCode: http.StatusInternalServerError,
|
||||
Message: "the server encountered a problem",
|
||||
}
|
||||
|
||||
var customErr *data.Error
|
||||
if errors.As(err, &customErr) {
|
||||
apiErr = customErr
|
||||
}
|
||||
|
||||
if err := common.WriteJSON(w, apiErr.HttpCode, common.Envelope{"error": apiErr}, nil); err != nil {
|
||||
api.App.LogError(r, err)
|
||||
w.WriteHeader(500)
|
||||
}
|
||||
}
|
||||
|
||||
func (api *Api) ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
||||
api.App.LogError(r, err)
|
||||
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
|
||||
@ -15,6 +15,6 @@ func (api *Api) Healthcheck(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := common.WriteJSON(w, http.StatusOK, common.Envelope{"health_check": env}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,12 +33,12 @@ func (api *Api) ListIssues(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
issues, metadata, err := api.App.FetchIssues(input.Title, input.Filters, common.GetUser(r))
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"issues": issues, "metadata": metadata}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,7 +65,7 @@ func (api *Api) CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
headers := make(http.Header)
|
||||
headers.Set("Location", fmt.Sprintf("/v1/issues/%d", issue.ID))
|
||||
if err = common.WriteJSON(w, http.StatusCreated, common.Envelope{"issue": issue, "options": options}, headers); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,13 +81,13 @@ func (api *Api) ReadIssue(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
} else {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"issue": result.IssueDetail, "options": result.Options}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,7 +117,7 @@ func (api *Api) UpdateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"issue": issue}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,13 +132,13 @@ func (api *Api) DeleteIssue(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
} else {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "issue successfully deleted"}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,13 +154,13 @@ func (api *Api) ReadIssuePubKey(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
} else {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"public_key": pubKey}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -186,6 +186,6 @@ func (api *Api) BlindSignIssueVote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"signed": signed}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ func (api *Api) RecoverPanic(next http.Handler) http.Handler {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
w.Header().Set("Connection", "close")
|
||||
api.ServerErrorResponse(w, r, fmt.Errorf("%s", err))
|
||||
api.errorResponse(w, r, fmt.Errorf("%s", err))
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
@ -49,7 +49,7 @@ func (api *Api) RequirePermission(code string, next http.HandlerFunc) http.Handl
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
permissions, err := api.App.Models.Permissions.GetAllForUser(common.GetUser(r).ID)
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
if !permissions.Include(code) {
|
||||
@ -87,7 +87,7 @@ func (api *Api) RateLimit(next http.Handler) http.Handler {
|
||||
if api.App.LimiterEnabled {
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
func (api *Api) ListMPs(w http.ResponseWriter, r *http.Request) {
|
||||
members, err := api.App.Parlament.ListNRMembers("XXVIII")
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -25,6 +25,6 @@ func (api *Api) ListMPs(w http.ResponseWriter, r *http.Request) {
|
||||
"members": members,
|
||||
"total": len(members),
|
||||
}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ func (api *Api) GetParlVoteDetail(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
detail, err := api.App.Parlament.GetDocumentVote(path)
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
if detail == nil {
|
||||
@ -30,6 +30,6 @@ func (api *Api) GetParlVoteDetail(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := common.WriteJSON(w, http.StatusOK, common.Envelope{"vote": detail}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ func (api *Api) ListParlVotes(w http.ResponseWriter, r *http.Request) {
|
||||
Period: []string{parlament.CurrentPeriod},
|
||||
})
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ func (api *Api) ListParlVotes(w http.ResponseWriter, r *http.Request) {
|
||||
"total_with_votes": totalWithVotes,
|
||||
"party_stats": partyStats,
|
||||
}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -35,13 +35,13 @@ func (api *Api) CreateAuthenticationToken(w http.ResponseWriter, r *http.Request
|
||||
case errors.Is(err, data.ErrInvalidCredentials):
|
||||
api.errorResponse(w, r, data.ErrInvalidCredentials)
|
||||
default:
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusCreated, common.Envelope{"authentication_token": token}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,11 +61,11 @@ func (api *Api) DeleteAuthenticationToken(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
|
||||
if err := api.App.DeleteToken(token); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "authentication token successfully deleted"}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,12 +30,12 @@ func (api *Api) ListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
users, metadata, err := api.App.ListUsers(input.Filters)
|
||||
if err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"users": users, "metadata": metadata}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ func (api *Api) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusCreated, common.Envelope{"user": user, "authentication_token": authToken}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,13 +80,13 @@ func (api *Api) ReadUser(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
} else {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"user": target}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ func (api *Api) ReadUser(w http.ResponseWriter, r *http.Request) {
|
||||
// user := common.GetUser(r).ID
|
||||
|
||||
// if err := common.WriteJSON(w, http.StatusOK, common.Envelope{"user": user}, nil); err != nil {
|
||||
// api.ServerErrorResponse(w, r, err)
|
||||
// api.errorResponse(w, r, err)
|
||||
// }
|
||||
// }
|
||||
|
||||
@ -109,13 +109,13 @@ func (api *Api) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
if errors.Is(err, data.ErrRecordNotFound) {
|
||||
api.errorResponse(w, r, data.ErrRecordNotFound)
|
||||
} else {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"message": "user successfully deleted"}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,6 +142,6 @@ func (api *Api) ActivateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err = common.WriteJSON(w, http.StatusOK, common.Envelope{"user": user}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,12 +26,12 @@ func (api *Api) Vote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := api.App.CastVote(input.IssueID, input.OptionID, input.Nonce, input.Signature); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err := common.WriteJSON(w, http.StatusCreated, common.Envelope{"message": "vote successfully cast"}, nil); err != nil {
|
||||
api.ServerErrorResponse(w, r, err)
|
||||
api.errorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user