...
1 package web
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "strings"
8
9 blog "github.com/letsencrypt/boulder/log"
10 "github.com/letsencrypt/boulder/probs"
11 )
12
13
14
15
16
17
18
19
20
21
22 func SendError(
23 log blog.Logger,
24 response http.ResponseWriter,
25 logEvent *RequestEvent,
26 prob *probs.ProblemDetails,
27 ierr error,
28 ) {
29
30 response.Header().Set("Content-Type", "application/problem+json")
31 if prob.HTTPStatus != 0 {
32 response.WriteHeader(prob.HTTPStatus)
33 } else {
34
35
36
37 response.WriteHeader(http.StatusInternalServerError)
38 }
39
40
41 logEvent.Error = fmt.Sprintf("%d :: %s :: %s", prob.HTTPStatus, prob.Type, prob.Detail)
42 if len(prob.SubProblems) > 0 {
43 subDetails := make([]string, len(prob.SubProblems))
44 for i, sub := range prob.SubProblems {
45 subDetails[i] = fmt.Sprintf("\"%s :: %s :: %s\"", sub.Identifier.Value, sub.Type, sub.Detail)
46 }
47 logEvent.Error += fmt.Sprintf(" [%s]", strings.Join(subDetails, ", "))
48 }
49 if ierr != nil {
50 logEvent.AddError(fmt.Sprintf("%s", ierr))
51 }
52
53
54 prob.Type = probs.ProblemType(probs.ErrorNS) + prob.Type
55 for i := range prob.SubProblems {
56 prob.SubProblems[i].Type = probs.ProblemType(probs.ErrorNS) + prob.SubProblems[i].Type
57 }
58
59 problemDoc, err := json.MarshalIndent(prob, "", " ")
60 if err != nil {
61 log.AuditErrf("Could not marshal error message: %s - %+v", err, prob)
62 problemDoc = []byte("{\"detail\": \"Problem marshalling error message.\"}")
63 }
64
65 response.Write(problemDoc)
66 }
67
View as plain text