mirror of
https://github.com/usememos/memos.git
synced 2024-12-20 01:31:29 +03:00
51 lines
845 B
Go
51 lines
845 B
Go
package e
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type ServerError struct {
|
|
Code int
|
|
Message string
|
|
}
|
|
|
|
type ErrorResponse struct {
|
|
Succeed bool `json:"succeed"`
|
|
Message string `json:"message"`
|
|
StatusCode int `json:"statusCode"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
func getServerError(err string) ServerError {
|
|
code, exists := Codes[err]
|
|
|
|
println(err)
|
|
|
|
if !exists {
|
|
err = "BAD_REQUEST"
|
|
code = 40000
|
|
}
|
|
|
|
return ServerError{
|
|
Code: code,
|
|
Message: err,
|
|
}
|
|
}
|
|
|
|
func ErrorHandler(w http.ResponseWriter, err string, message string) {
|
|
serverError := getServerError(err)
|
|
|
|
res := ErrorResponse{
|
|
Succeed: false,
|
|
Message: message,
|
|
StatusCode: serverError.Code,
|
|
Data: nil,
|
|
}
|
|
|
|
statusCode := int(serverError.Code / 100)
|
|
|
|
w.WriteHeader(statusCode)
|
|
json.NewEncoder(w).Encode(res)
|
|
}
|