diff --git a/src/validation/errors.go b/src/validation/errors.go index b0dfb3c7..183faf20 100644 --- a/src/validation/errors.go +++ b/src/validation/errors.go @@ -2,6 +2,7 @@ package validation import ( "errors" + "net/http" ) // Error represents a validation error @@ -214,6 +215,44 @@ var ( ErrStatusUnknown = Error{errors.New("got unknown status from tibia.com")} ) +func (e Error) StatusCode(httpCode int) int { + switch e { + case ErrorWorldDoesNotExist: + return http.StatusNotFound + case ErrorVocationDoesNotExist: + return http.StatusNotFound + case ErrorHouseDoesNotExist: + return http.StatusNotFound + case ErrorTownDoesNotExist: + return http.StatusNotFound + case ErrorHighscoreCategoryDoesNotExist: + return http.StatusNotFound + + case ErrorCharacterNotFound: + return http.StatusNotFound + case ErrorCreatureNotFound: + return http.StatusNotFound + case ErrorSpellNotFound: + return http.StatusNotFound + case ErrorGuildNotFound: + return http.StatusNotFound + case ErrorMaintenanceMode: + // An error occurred at tibia.com + return http.StatusBadGateway + case ErrStatusForbidden: + // An error occurred at tibia.com + return http.StatusBadGateway + case ErrStatusFound: + // An error occurred at tibia.com + return http.StatusBadGateway + case ErrStatusUnknown: + // An error occurred at tibia.com + return http.StatusBadGateway + default: + return httpCode + } +} + // Code will return the code of the error func (e Error) Code() int { switch e { diff --git a/src/webserver.go b/src/webserver.go index b3210069..973ca3fe 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -1052,7 +1052,7 @@ func tibiaWorldsWorld(c *gin.Context) { } if !exists { - TibiaDataErrorHandler(c, validation.ErrorWorldDoesNotExist, http.StatusBadRequest) + TibiaDataErrorHandler(c, validation.ErrorWorldDoesNotExist, http.StatusNotFound) return } @@ -1093,10 +1093,7 @@ func TibiaDataErrorHandler(c *gin.Context, err error, httpCode int) { } } - // An error occurred at tibia.com - if t.Code() > 20000 { - httpCode = http.StatusBadGateway - } + httpCode = t.StatusCode(httpCode) info.Status.HTTPCode = httpCode info.Status.Error = t.Code() @@ -1120,6 +1117,7 @@ func TibiaDataErrorHandler(c *gin.Context, err error, httpCode int) { func tibiaDataRequestHandler(c *gin.Context, tibiaDataRequest TibiaDataRequestStruct, requestHandler func(string) (interface{}, error), handlerName string) { BoxContentHTML, err := TibiaDataHTMLDataCollector(tibiaDataRequest) + // return error (e.g. for maintenance mode) if err != nil { TibiaDataErrorHandler(c, err, http.StatusBadGateway)