-
Notifications
You must be signed in to change notification settings - Fork 672
/
response.go
40 lines (33 loc) · 1017 Bytes
/
response.go
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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package auth
import (
"encoding/json"
"net/http"
rpc "github.com/gorilla/rpc/v2/json2"
)
type responseErr struct {
Code rpc.ErrorCode `json:"code"`
Message string `json:"message"`
}
type responseBody struct {
Version string `json:"jsonrpc"`
Err responseErr `json:"error"`
ID uint8 `json:"id"`
}
// Write a JSON-RPC formatted response saying that the API call is unauthorized.
// The response has header http.StatusUnauthorized.
// Errors while writing are ignored.
func writeUnauthorizedResponse(w http.ResponseWriter, err error) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
// There isn't anything to do with the returned error, so it is dropped.
_ = json.NewEncoder(w).Encode(responseBody{
Version: rpc.Version,
Err: responseErr{
Code: rpc.E_INVALID_REQ,
Message: err.Error(),
},
ID: 1,
})
}