remote-task-excutor-cli/pkg/handler/response.go

39 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import "net/http"
// Response 统一的 API 响应格式,成功与失败均使用 code、msg、data接口失败时错误信息通过 msg 返回。
type Response struct {
Code int `json:"code"` // 状态码
Msg string `json:"msg"` // 提示信息(成功可为空或 success失败时为错误信息
Data interface{} `json:"data"` // 数据对象,失败时为 null
}
// SuccessResponse 成功响应
func SuccessResponse(data interface{}) Response {
return Response{
Code: http.StatusOK,
Msg: "success",
Data: data,
}
}
// ErrorResponse 错误响应,错误信息通过 msg 返回
func ErrorResponse(code int, msg string) Response {
return Response{
Code: code,
Msg: msg,
Data: nil,
}
}
// BadRequestResponse 400 错误响应,错误信息通过 msg 返回
func BadRequestResponse(msg string) Response {
return ErrorResponse(http.StatusBadRequest, msg)
}
// InternalServerErrorResponse 500 错误响应,错误信息通过 msg 返回
func InternalServerErrorResponse(msg string) Response {
return ErrorResponse(http.StatusInternalServerError, msg)
}