Files
hatchet/pkg/integrations/shared/webhookutils/errors.go
Luca Steeb c8f87599c7 chore: ignore or fix linter errors (#116)
* chore: ignore or fix linter errors

* chore: wrap up lint errors

* chore: sqlc generation

---------

Co-authored-by: Alexander Belanger <belanger@sas.upenn.edu>
2024-01-21 22:01:20 -05:00

76 lines
1.3 KiB
Go

package webhookutils
import (
"encoding/json"
"errors"
"net/http"
"github.com/rs/zerolog"
hatcheterrors "github.com/hatchet-dev/hatchet/pkg/errors"
)
type ErrorOpts struct {
Code uint
}
func HandleAPIError(
l *zerolog.Logger,
alerter hatcheterrors.Alerter,
w http.ResponseWriter,
r *http.Request,
err error,
writeErr bool,
) {
// if the error is of type detailed error, get the code from that
detailedErr := hatcheterrors.DetailedError{}
if ok := errors.As(err, &detailedErr); ok {
if detailedErr.Code == 0 || detailedErr.Code >= http.StatusInternalServerError {
handleInternalError(
l,
alerter,
w,
r,
detailedErr,
writeErr,
)
} else {
w.WriteHeader(int(detailedErr.Code))
writerErr := json.NewEncoder(w).Encode(detailedErr)
if writerErr != nil {
handleInternalError(
l,
alerter,
w,
r,
writerErr,
false,
)
}
}
}
}
func handleInternalError(l *zerolog.Logger,
alerter hatcheterrors.Alerter,
w http.ResponseWriter,
r *http.Request,
err error,
writeErr bool) {
event := l.Warn().
Str("internal_error", err.Error())
event.Send()
data := make(map[string]interface{})
data["method"] = r.Method
data["url"] = r.URL.String()
alerter.SendAlert(r.Context(), err, data)
w.WriteHeader(http.StatusInternalServerError)
}