44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package errcode
|
|
|
|
type Error struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func (e *Error) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
func NewError(code int, message string) *Error {
|
|
return &Error{
|
|
Code: code,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
var (
|
|
// 通用错误
|
|
ErrBadRequest = NewError(10001, "Bad Request")
|
|
ErrUnauthorized = NewError(10002, "Unauthorized")
|
|
ErrForbidden = NewError(10003, "Forbidden")
|
|
ErrNotFound = NewError(10004, "Not Found")
|
|
ErrInternalServerError = NewError(10005, "Internal Server Error")
|
|
|
|
// 用户相关错误
|
|
ErrInvalidUsername = NewError(20001, "Invalid Username")
|
|
ErrInvalidPassword = NewError(20002, "Invalid Password")
|
|
ErrUserAlreadyExists = NewError(20003, "User Already Exists")
|
|
ErrUserNotFound = NewError(20004, "User Not Found")
|
|
ErrInvalidToken = NewError(20005, "Invalid Token")
|
|
ErrUsernameExists = NewError(20006, "Username Already Exists")
|
|
ErrEmailExists = NewError(20007, "Email Already Exists")
|
|
|
|
// 帖子相关错误
|
|
ErrPostNotFound = NewError(30001, "Post Not Found")
|
|
ErrInvalidTitle = NewError(30002, "Invalid Title")
|
|
ErrInvalidContent = NewError(30003, "Invalid Content")
|
|
|
|
// 其他错误
|
|
ErrDatabaseError = NewError(40001, "Database Error")
|
|
)
|