mirror of
https://github.com/harness/drone.git
synced 2025-05-06 12:41:08 +08:00
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
// that can be found in the LICENSE file.
|
|
|
|
package render
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/drone/drone/handler/api/errors"
|
|
)
|
|
|
|
// indent the json-encoded API responses
|
|
var indent bool
|
|
|
|
func init() {
|
|
indent, _ = strconv.ParseBool(
|
|
os.Getenv("HTTP_JSON_INDENT"),
|
|
)
|
|
}
|
|
|
|
var (
|
|
// ErrInvalidToken is returned when the api request token is invalid.
|
|
ErrInvalidToken = errors.New("Invalid or missing token")
|
|
|
|
// ErrUnauthorized is returned when the user is not authorized.
|
|
ErrUnauthorized = errors.New("Unauthorized")
|
|
|
|
// ErrForbidden is returned when user access is forbidden.
|
|
ErrForbidden = errors.New("Forbidden")
|
|
|
|
// ErrNotFound is returned when a resource is not found.
|
|
ErrNotFound = errors.New("Not Found")
|
|
)
|
|
|
|
// ErrorCode writes the json-encoded error message to the response.
|
|
func ErrorCode(w http.ResponseWriter, err error, status int) {
|
|
JSON(w, &errors.Error{Message: err.Error()}, status)
|
|
}
|
|
|
|
// InternalError writes the json-encoded error message to the response
|
|
// with a 500 internal server error.
|
|
func InternalError(w http.ResponseWriter, err error) {
|
|
ErrorCode(w, err, 500)
|
|
}
|
|
|
|
// InternalErrorf writes the json-encoded error message to the response
|
|
// with a 500 internal server error.
|
|
func InternalErrorf(w http.ResponseWriter, format string, a ...interface{}) {
|
|
ErrorCode(w, fmt.Errorf(format, a...), 500)
|
|
}
|
|
|
|
// NotFound writes the json-encoded error message to the response
|
|
// with a 404 not found status code.
|
|
func NotFound(w http.ResponseWriter, err error) {
|
|
ErrorCode(w, err, 404)
|
|
}
|
|
|
|
// NotFoundf writes the json-encoded error message to the response
|
|
// with a 404 not found status code.
|
|
func NotFoundf(w http.ResponseWriter, format string, a ...interface{}) {
|
|
ErrorCode(w, fmt.Errorf(format, a...), 404)
|
|
}
|
|
|
|
// Unauthorized writes the json-encoded error message to the response
|
|
// with a 401 unauthorized status code.
|
|
func Unauthorized(w http.ResponseWriter, err error) {
|
|
ErrorCode(w, err, 401)
|
|
}
|
|
|
|
// Forbidden writes the json-encoded error message to the response
|
|
// with a 403 forbidden status code.
|
|
func Forbidden(w http.ResponseWriter, err error) {
|
|
ErrorCode(w, err, 403)
|
|
}
|
|
|
|
// BadRequest writes the json-encoded error message to the response
|
|
// with a 400 bad request status code.
|
|
func BadRequest(w http.ResponseWriter, err error) {
|
|
ErrorCode(w, err, 400)
|
|
}
|
|
|
|
// BadRequestf writes the json-encoded error message to the response
|
|
// with a 400 bad request status code.
|
|
func BadRequestf(w http.ResponseWriter, format string, a ...interface{}) {
|
|
ErrorCode(w, fmt.Errorf(format, a...), 400)
|
|
}
|
|
|
|
// JSON writes the json-encoded error message to the response
|
|
// with a 400 bad request status code.
|
|
func JSON(w http.ResponseWriter, v interface{}, status int) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
enc := json.NewEncoder(w)
|
|
if indent {
|
|
enc.SetIndent("", " ")
|
|
}
|
|
enc.Encode(v)
|
|
}
|