oceanbase
81 строка · 2.1 Кб
1/**
2* Copyright (c) 2021 OceanBase
3* OceanBase CE is licensed under Mulan PubL v2.
4* You can use this software according to the terms and conditions of the Mulan PubL v2.
5* You may obtain a copy of Mulan PubL v2 at:
6* http://license.coscl.org.cn/MulanPubL-2.0
7* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10* See the Mulan PubL v2 for more details.
11*/
12
13package server
14
15import (
16"fmt"
17"net/http"
18)
19
20type ApiResponse struct {
21Code int `json:"Code"`
22Message string `json:"Message"`
23Successful bool `json:"Success"`
24Data interface{} `json:"Data"`
25TraceId string `json:"Trace"`
26Server string `json:"Server"`
27Cost int64 `json:"Cost"`
28}
29
30type IterableData struct {
31Contents interface{} `json:"Contents"`
32}
33
34func NewSuccessResponse(data interface{}) *ApiResponse {
35return &ApiResponse{
36Code: http.StatusOK,
37Message: "successful",
38Successful: true,
39Data: data,
40}
41}
42
43func NewBadRequestResponse(err error) *ApiResponse {
44return &ApiResponse{
45Code: http.StatusBadRequest,
46Message: fmt.Sprintf("bad request: %v", err),
47Successful: false,
48}
49}
50
51func NewIllegalArgumentResponse(err error) *ApiResponse {
52return &ApiResponse{
53Code: http.StatusBadRequest,
54Message: fmt.Sprintf("illegal argument: %v", err),
55Successful: false,
56}
57}
58
59func NewNotFoundResponse(err error) *ApiResponse {
60return &ApiResponse{
61Code: http.StatusNotFound,
62Message: fmt.Sprintf("resource not found: %v", err),
63Successful: false,
64}
65}
66
67func NewNotImplementedResponse(err error) *ApiResponse {
68return &ApiResponse{
69Code: http.StatusNotImplemented,
70Message: fmt.Sprintf("request not implemented: %v", err),
71Successful: false,
72}
73}
74
75func NewErrorResponse(err error) *ApiResponse {
76return &ApiResponse{
77Code: http.StatusInternalServerError,
78Message: fmt.Sprintf("got internal error: %v", err),
79Successful: false,
80}
81}
82