201 lines
6.6 KiB
Go
201 lines
6.6 KiB
Go
// Sofaraum server is the server which collects the statistics
|
|
// for the sofaraum-heatmap application.
|
|
// Copyright 2018 K.Langenberg and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package models
|
|
|
|
import (
|
|
"code.vikunja.io/web"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// ErrInvalidData represents a "ErrInvalidData" kind of error. Used when a struct is invalid -> validation failed.
|
|
type ErrInvalidData struct {
|
|
Message string
|
|
}
|
|
|
|
// IsErrInvalidData checks if an error is a ErrIDCannotBeZero.
|
|
func IsErrInvalidData(err error) bool {
|
|
_, ok := err.(ErrInvalidData)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrInvalidData) Error() string {
|
|
return fmt.Sprintf("Struct is invalid. %s", err.Message)
|
|
}
|
|
|
|
// ErrCodeInvalidData holds the unique world-error code of this error
|
|
const ErrCodeInvalidData = 1000
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrInvalidData) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusBadRequest, Code: ErrCodeInvalidData, Message: err.Message}
|
|
}
|
|
|
|
// ValidationHTTPError is the http error when a validation fails
|
|
type ValidationHTTPError struct {
|
|
web.HTTPError
|
|
InvalidFields []string `json:"invalid_fields"`
|
|
}
|
|
|
|
// Error implements the Error type (so we can return it as type error)
|
|
func (err ValidationHTTPError) Error() string {
|
|
theErr := ErrInvalidData{
|
|
Message: err.Message,
|
|
}
|
|
return theErr.Error()
|
|
}
|
|
|
|
// =============
|
|
// User Errors
|
|
// =============
|
|
|
|
// ErrUserDoesNotExist represents a "UserDoesNotExist" kind of error.
|
|
type ErrUserDoesNotExist struct {
|
|
UserID int64
|
|
}
|
|
|
|
// IsErrUserDoesNotExist checks if an error is a ErrUserDoesNotExist.
|
|
func IsErrUserDoesNotExist(err error) bool {
|
|
_, ok := err.(ErrUserDoesNotExist)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrUserDoesNotExist) Error() string {
|
|
return fmt.Sprintf("User does not exist [user id: %d]", err.UserID)
|
|
}
|
|
|
|
// ErrCodeUserDoesNotExist holds the unique world-error code of this error
|
|
const ErrCodeUserDoesNotExist = 2000
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrUserDoesNotExist) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusNotFound, Code: ErrCodeUserDoesNotExist, Message: "The user does not exist."}
|
|
}
|
|
|
|
// ErrNoUsernamePassword represents a "NoUsernamePassword" kind of error.
|
|
type ErrNoUsernamePassword struct{}
|
|
|
|
// IsErrNoUsernamePassword checks if an error is a ErrNoUsernamePassword.
|
|
func IsErrNoUsernamePassword(err error) bool {
|
|
_, ok := err.(ErrNoUsernamePassword)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrNoUsernamePassword) Error() string {
|
|
return fmt.Sprintf("No username and password provided")
|
|
}
|
|
|
|
// ErrCodeNoUsernamePassword holds the unique world-error code of this error
|
|
const ErrCodeNoUsernamePassword = 2001
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrNoUsernamePassword) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusBadRequest, Code: ErrCodeNoUsernamePassword, Message: "Please specify a username and a password."}
|
|
}
|
|
|
|
// ErrWrongUsernameOrPassword is an error where the email was not confirmed
|
|
type ErrWrongUsernameOrPassword struct {
|
|
}
|
|
|
|
func (err ErrWrongUsernameOrPassword) Error() string {
|
|
return fmt.Sprintf("Wrong username or password")
|
|
}
|
|
|
|
// ErrCodeWrongUsernameOrPassword holds the unique world-error code of this error
|
|
const ErrCodeWrongUsernameOrPassword = 2002
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrWrongUsernameOrPassword) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusPreconditionFailed, Code: ErrCodeWrongUsernameOrPassword, Message: "Wrong username or password."}
|
|
}
|
|
|
|
// IsErrWrongUsernameOrPassword checks if an error is a IsErrEmailNotConfirmed.
|
|
func IsErrWrongUsernameOrPassword(err error) bool {
|
|
_, ok := err.(ErrWrongUsernameOrPassword)
|
|
return ok
|
|
}
|
|
|
|
// ErrCouldNotGetUserID represents a "ErrCouldNotGetUserID" kind of error.
|
|
type ErrCouldNotGetUserID struct{}
|
|
|
|
// IsErrCouldNotGetUserID checks if an error is a ErrCouldNotGetUserID.
|
|
func IsErrCouldNotGetUserID(err error) bool {
|
|
_, ok := err.(ErrCouldNotGetUserID)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrCouldNotGetUserID) Error() string {
|
|
return fmt.Sprintf("Could not get user ID")
|
|
}
|
|
|
|
// ErrCodeCouldNotGetUserID holds the unique world-error code of this error
|
|
const ErrCodeCouldNotGetUserID = 2003
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrCouldNotGetUserID) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusBadRequest, Code: ErrCodeCouldNotGetUserID, Message: "Could not get user id."}
|
|
}
|
|
|
|
// ErrUsernameExists represents a "UsernameAlreadyExists" kind of error.
|
|
type ErrUsernameExists struct {
|
|
UserID int64
|
|
Username string
|
|
}
|
|
|
|
// IsErrUsernameExists checks if an error is a ErrUsernameExists.
|
|
func IsErrUsernameExists(err error) bool {
|
|
_, ok := err.(ErrUsernameExists)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrUsernameExists) Error() string {
|
|
return fmt.Sprintf("User with that username already exists [user id: %d, username: %s]", err.UserID, err.Username)
|
|
}
|
|
|
|
// ErrorCodeUsernameExists holds the unique world-error code of this error
|
|
const ErrorCodeUsernameExists = 2004
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrUsernameExists) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusBadRequest, Code: ErrorCodeUsernameExists, Message: "A user with this username already exists."}
|
|
}
|
|
|
|
// ErrUserEmailExists represents a "UserEmailExists" kind of error.
|
|
type ErrUserEmailExists struct {
|
|
UserID int64
|
|
Email string
|
|
}
|
|
|
|
// IsErrUserEmailExists checks if an error is a ErrUserEmailExists.
|
|
func IsErrUserEmailExists(err error) bool {
|
|
_, ok := err.(ErrUserEmailExists)
|
|
return ok
|
|
}
|
|
|
|
func (err ErrUserEmailExists) Error() string {
|
|
return fmt.Sprintf("User with that email already exists [user id: %d, email: %s]", err.UserID, err.Email)
|
|
}
|
|
|
|
// ErrorCodeUserEmailExists holds the unique world-error code of this error
|
|
const ErrorCodeUserEmailExists = 2005
|
|
|
|
// HTTPError holds the http error description
|
|
func (err ErrUserEmailExists) HTTPError() web.HTTPError {
|
|
return web.HTTPError{HTTPCode: http.StatusBadRequest, Code: ErrorCodeUserEmailExists, Message: "A user with this email address already exists."}
|
|
}
|