Skip to content

Commit

Permalink
feat: I fix the issue TibiaData#311 the wrong code status in endpoint…
Browse files Browse the repository at this point in the history
…s when is not found or not exist
  • Loading branch information
Pvmendes committed Mar 5, 2024
1 parent d74426c commit 5457b80
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/validation/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validation

import (
"errors"
"net/http"
)

// Error represents a validation error
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions src/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ func tibiaWorldsWorld(c *gin.Context) {
}

if !exists {
TibiaDataErrorHandler(c, validation.ErrorWorldDoesNotExist, http.StatusBadRequest)
TibiaDataErrorHandler(c, validation.ErrorWorldDoesNotExist, http.StatusNotFound)
return
}

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 5457b80

Please sign in to comment.