60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package api_acladmin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
ErrorInvalidRequestBody = "INVALID_REQUEST_BODY"
|
|
ErrorInternalServerError = "INTERNAL_SERVER_ERROR"
|
|
|
|
// Roles
|
|
ErrorFailedToCreateRole = "FAILED_TO_CREATE_ROLE"
|
|
ErrorFailedToGetRole = "FAILED_TO_GET_ROLE"
|
|
ErrorFailedToUpdateRole = "FAILED_TO_UPDATE_ROLE"
|
|
ErrorFailedToDeleteRole = "FAILED_TO_DELETE_ROLE"
|
|
|
|
ErrorInvalidRoleID = "INVALID_ROLE_ID"
|
|
ErrorRoleNotFound = "ROLE_NOT_FOUND"
|
|
|
|
// Resources
|
|
ErrorFailedToCreateResource = "FAILED_TO_CREATE_RESOURCE"
|
|
ErrorFailedToGetResource = "FAILED_TO_GET_RESOURCE"
|
|
ErrorFailedToUpdateResource = "FAILED_TO_UPDATE_RESOURCE"
|
|
ErrorFailedToDeleteResource = "FAILED_TO_DELETE_RESOURCE"
|
|
|
|
ErrorInvalidResourceID = "INVALID_RESOURCE_ID"
|
|
ErrorResourceNotFound = "RESOURCE_NOT_FOUND"
|
|
)
|
|
|
|
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)
|
|
}
|