some changes
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package api_acladmin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
ErrorInvalidRequestBody = "INVALID_REQUEST_BODY"
|
||||
ErrorInternalServerError = "INTERNAL_SERVER_ERROR"
|
||||
@@ -26,3 +32,28 @@ 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user