forked from Azure/aad-auth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfail_request.go
More file actions
42 lines (36 loc) · 1.2 KB
/
Copy pathfail_request.go
File metadata and controls
42 lines (36 loc) · 1.2 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
package handler
import (
"aad-auth-proxy/constants"
"context"
"encoding/json"
"net/http"
"strconv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
// Fail request
func FailRequest(w http.ResponseWriter, r *http.Request, statusCode int, errorCode string, ctx context.Context, err error) {
_, span := otel.Tracer(constants.SERVICE_TELEMETRY_KEY).Start(ctx, "failRequest")
defer span.End()
errorMessage := err.Error()
requestId := r.Header.Get(constants.HEADER_REQUEST_ID)
span.SetAttributes(
attribute.Int("response.status_code", statusCode),
attribute.String("response.request_id", requestId),
attribute.String("response.error.code", errorCode),
attribute.String("response.error.message", errorMessage),
)
span.SetStatus(codes.Error, "failed to forward request")
w.WriteHeader(statusCode)
w.Header().Add(constants.HEADER_CONTENT_TYPE, "application/json")
w.Header().Add(constants.HEADER_STATUS_CODE, strconv.Itoa(statusCode))
resp := make(map[string]string)
resp[constants.ERROR_PROPERTY_CODE] = errorCode
resp[constants.ERROR_PROPERTY_MESSAGE] = errorMessage
jsonResp, err := json.Marshal(resp)
if err != nil {
return
}
w.Write(jsonResp)
}