Skip to content

Commit e773871

Browse files
committed
add context handler with errors
1 parent a2a5a23 commit e773871

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

cmd/tpl/deployment.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ GOOS=$(shell go env GOOS)
2424
GOARCH=$(shell go env GOARCH)
2525
GOPRIVATE=github.com/SencilloDev
2626
27-
# Version and image repo are overriden by the ci pipeline
28-
VERSION=x.x.x
29-
IMAGE_REPO=local/${shell basename ${PWD}}
30-
IMAGE=${IMAGE_REPO}:${VERSION}
31-
TEST_IMAGE:=${IMAGE_REPO}-test:${VERSION}
32-
3327
.PHONY: all build docker deps clean test coverage lint docker-local edgedb k8s-up k8s-down docker-delete docs update-local deploy-local
3428
3529
all: build
@@ -57,7 +51,7 @@ tidy: ## Pull in dependencies
5751
fmt: ## Format All files
5852
{{"\t"}}go fmt ./...
5953
60-
{{ .Name }}ctl: ## Builds the binary on the current platform
54+
build: ## Builds the binary on the current platform
6155
{{"\t"}}go build -mod=vendor -a -ldflags "-w -X '$(PKG)/cmd.Version=$(VERSION)'" -o $(PROJECT_NAME)ctl
6256
6357
docs: ## Builds the cli documentation
@@ -217,7 +211,7 @@ jobs:
217211

218212
func Gitignore() []byte {
219213
return []byte(`{{ .Name }}ctl
220-
cwgotctl*
214+
sgoctl*
221215
dist/
222216
output/
223217
`)

transports/http/router.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import (
2525
"syscall"
2626
"time"
2727

28+
sderrors "github.com/SencilloDev/sencillo-go/errors"
2829
"github.com/SencilloDev/sencillo-go/metrics"
29-
cwmiddleware "github.com/SencilloDev/sencillo-go/transports/http/middleware"
30+
sdmiddleware "github.com/SencilloDev/sencillo-go/transports/http/middleware"
3031
"github.com/prometheus/client_golang/prometheus"
3132
"github.com/prometheus/client_golang/prometheus/promhttp"
3233
"github.com/sagikazarmark/slog-shim"
@@ -112,6 +113,26 @@ func HandleWithContext[T any](h func(http.ResponseWriter, *http.Request, T), ctx
112113
}
113114
}
114115

116+
func HandleWithContextError[T any](h func(http.ResponseWriter, *http.Request, T) error, ctx T, logger *slog.Logger) http.HandlerFunc {
117+
return func(w http.ResponseWriter, r *http.Request) {
118+
err := h(w, r, ctx)
119+
if err == nil {
120+
return
121+
}
122+
123+
var ce sderrors.ClientError
124+
if errors.As(err, &ce) {
125+
w.WriteHeader(ce.Status)
126+
w.Write([]byte(ce.Body()))
127+
return
128+
}
129+
130+
logger.Error(fmt.Sprintf("status=%d, err=%v", http.StatusInternalServerError, err))
131+
w.WriteHeader(http.StatusInternalServerError)
132+
w.Write([]byte(ErrInternalError.Error()))
133+
}
134+
}
135+
115136
func (s *Server) getHealth() {
116137
if s.TracerProvider != nil {
117138
s.Router.Handle("GET /healthz", otelhttp.NewHandler(http.HandlerFunc(healthz), "healthz:GET"))
@@ -161,7 +182,7 @@ func (e *ErrHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
161182
return
162183
}
163184

164-
var ce *ClientError
185+
var ce sderrors.ClientError
165186
if errors.As(err, &ce) {
166187
w.WriteHeader(ce.Status)
167188
w.Write([]byte(ce.Body()))
@@ -191,7 +212,7 @@ func (s *Server) RegisterSubRouter(prefix string, routes []Route, middleware ...
191212
counter := metrics.NewCounterVec(fmt.Sprintf("http_requests%s", name), "HTTP requests by status, path, and method", []string{"code", "method", "path"})
192213
hist := metrics.NewHistogramVec(fmt.Sprintf("http_request_latency%s", name), "HTTP latency by status, path, and method", []string{"code", "method", "path"})
193214

194-
reqWrapped := cwmiddleware.RequestID(subRouter)
215+
reqWrapped := sdmiddleware.RequestID(subRouter)
195216

196217
for _, m := range middleware {
197218
reqWrapped = m(reqWrapped)
@@ -209,7 +230,7 @@ func (s *Server) RegisterSubRouter(prefix string, routes []Route, middleware ...
209230

210231
s.Exporter.Metrics = append(s.Exporter.Metrics, counter, hist)
211232

212-
s.Router.Handle(prefixWithSlash, cwmiddleware.Logging(cwmiddleware.CodeStats(http.StripPrefix(stripped, reqWrapped), counter, hist)))
233+
s.Router.Handle(prefixWithSlash, sdmiddleware.Logging(sdmiddleware.CodeStats(http.StripPrefix(stripped, reqWrapped), counter, hist)))
213234

214235
return s
215236
}

0 commit comments

Comments
 (0)