/
utils.go
39 lines (32 loc) · 829 Bytes
/
utils.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
package google
import (
"context"
"encoding/base64"
"encoding/json"
"math/rand"
"net/http"
"github.com/bobinette/papernet/errors"
)
var (
errInvalidRequest = errors.New("invalid request")
)
func randToken(size int) string {
b := make([]byte, size)
rand.Read(b)
return base64.StdEncoding.EncodeToString(b)
}
// Server defines the interface to register the http handlers.
type Server interface {
RegisterHandler(path, method string, f http.Handler)
}
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
statusCode := http.StatusInternalServerError
if err, ok := err.(errors.Error); ok {
statusCode = err.Code()
}
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
}