From 14689375898f271d6bed047a9cb8062fb5bb4256 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sat, 3 Jan 2026 15:39:42 +0200 Subject: [PATCH] move ProblemDetails --- api/acl_admin/errors.go | 31 ------------------------------- internal/server/error.go | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/api/acl_admin/errors.go b/api/acl_admin/errors.go index 642042d..dee7889 100644 --- a/api/acl_admin/errors.go +++ b/api/acl_admin/errors.go @@ -1,11 +1,5 @@ package api_acladmin -import ( - "encoding/json" - "log/slog" - "net/http" -) - const ( ErrorInvalidRequestBody = "INVALID_REQUEST_BODY" ErrorInternalServerError = "INTERNAL_SERVER_ERROR" @@ -32,28 +26,3 @@ const ( const ( ErrorACLServiceNotInitialized = "ACL service is not initialized" ) - -// RFC-7807 (Problem Details) -type ProblemDetails struct { - Type string `json:"type" example:"https://api.triggerssmith.com/errors/role-not-found"` - Title string `json:"title" example:"Role not found"` - Status int `json:"status" example:"404"` - Detail string `json:"detail" example:"No role with ID 42"` - Instance string `json:"instance" example:"/api/acl/roles/42"` -} - -var typeDomain = "https://api.triggerssmith.com" - -func writeProblem(w http.ResponseWriter, status int, typ, title, detail string, r *http.Request) { - w.Header().Set("Content-Type", "application/problem+json") - w.WriteHeader(status) - prob := ProblemDetails{ - Type: typeDomain + typ, - Title: title, - Status: status, - Detail: detail, - Instance: r.URL.Path, - } - slog.Warn("new problem", "type", typ, "title", title, "detail", detail, "instance", r.URL.Path, "status", status) - _ = json.NewEncoder(w).Encode(prob) -} diff --git a/internal/server/error.go b/internal/server/error.go index 83876c9..52bc954 100644 --- a/internal/server/error.go +++ b/internal/server/error.go @@ -2,6 +2,7 @@ package server import ( "encoding/json" + "log/slog" "net/http" ) @@ -18,3 +19,28 @@ func WriteError(w http.ResponseWriter, error, details string, statusCode int) { Details: details, }) } + +// RFC-7807 (Problem Details) +type ProblemDetails struct { + Type string `json:"type" example:"https://api.triggerssmith.com/errors/role-not-found"` + Title string `json:"title" example:"Role not found"` + Status int `json:"status" example:"404"` + Detail string `json:"detail" example:"No role with ID 42"` + Instance string `json:"instance" example:"/api/acl/roles/42"` +} + +var typeDomain = "https://api.triggerssmith.com" + +func WriteProblem(w http.ResponseWriter, status int, typ, title, detail string, r *http.Request) { + w.Header().Set("Content-Type", "application/problem+json") + w.WriteHeader(status) + prob := ProblemDetails{ + Type: typeDomain + typ, + Title: title, + Status: status, + Detail: detail, + Instance: r.URL.Path, + } + slog.Warn("new problem", "type", typ, "title", title, "detail", detail, "instance", r.URL.Path, "status", status) + _ = json.NewEncoder(w).Encode(prob) +}