-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_problem.go
More file actions
74 lines (69 loc) · 1.84 KB
/
response_problem.go
File metadata and controls
74 lines (69 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Package gin 提供基于 Gin 的增强上下文与相关组件。
package gin
import (
"encoding/json"
"net/http"
)
const problemJSONContentType = "application/problem+json; charset=utf-8"
// ProblemDetail 定义 RFC 9457 风格的标准错误模型。
type ProblemDetail struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Status int `json:"status,omitempty"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
RequestID string `json:"request_id,omitempty"`
Errors []ValidationError `json:"errors,omitempty"`
Extensions map[string]any `json:"-"`
}
// MarshalJSON 将扩展字段按 RFC 9457 约定平铺到顶层。
func (p ProblemDetail) MarshalJSON() ([]byte, error) {
payload := make(map[string]any, 7+len(p.Extensions))
if p.Type != "" {
payload["type"] = p.Type
}
if p.Title != "" {
payload["title"] = p.Title
}
if p.Status > 0 {
payload["status"] = p.Status
}
if p.Detail != "" {
payload["detail"] = p.Detail
}
if p.Instance != "" {
payload["instance"] = p.Instance
}
if p.RequestID != "" {
payload["request_id"] = p.RequestID
}
if len(p.Errors) > 0 {
payload["errors"] = p.Errors
}
for key, value := range p.Extensions {
if _, exists := payload[key]; exists {
continue
}
payload[key] = value
}
return json.Marshal(payload)
}
func newProblemDetail(status int, typeURI, title, detail, instance, requestID string) ProblemDetail {
if status <= 0 {
status = http.StatusInternalServerError
}
if typeURI == "" {
typeURI = "about:blank"
}
if title == "" {
title = http.StatusText(status)
}
return ProblemDetail{
Type: typeURI,
Title: title,
Status: status,
Detail: detail,
Instance: instance,
RequestID: requestID,
}
}