Skip to content

Commit d0e29c8

Browse files
authored
webserver/validation: improve error handling on non OK status codes (#301)
* webserver/validation: improve error handling on non OK status codes * webserver/validation: handle unknown status codes * webserver: send StatusBadGateway in case there was an error on tibia.com * webserver: fix typo in comment
1 parent 7b2f605 commit d0e29c8

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

src/validation/errors.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ var (
197197
// ErrorMaintenanceMode will be sent if there is ongoing maintenance
198198
// Code: 20005
199199
ErrorMaintenanceMode = Error{errors.New("maintenance mode active")}
200+
201+
// ErrStatusForbidden will be sent if tibia sent us a 403 response.
202+
// This usually happens when we are rate limited.
203+
// Code: 20006
204+
ErrStatusForbidden = Error{errors.New("got status forbidden from tibia.com")}
205+
206+
// ErrStatusForbidden will be sent if tibia sent us a 302 response, but it
207+
// is not in MaintenanceMode. Because if it were, we would throw a
208+
// ErrorMaintenanceMode.
209+
// Code: 20007
210+
ErrStatusFound = Error{errors.New("got status found from tibia.com")}
211+
212+
// ErrStatusUnknown will be sent a HTTP request we are not expecting.
213+
// Code: 20008
214+
ErrStatusUnknown = Error{errors.New("got unknown status from tibia.com")}
200215
)
201216

202217
// Code will return the code of the error
@@ -290,6 +305,12 @@ func (e Error) Code() int {
290305
return 20004
291306
case ErrorMaintenanceMode:
292307
return 20005
308+
case ErrStatusForbidden:
309+
return 20006
310+
case ErrStatusFound:
311+
return 20007
312+
case ErrStatusUnknown:
313+
return 20008
293314
default:
294315
return 0
295316
}

src/webserver.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"log"
89
"net/http"
910
"os"
@@ -1087,13 +1088,17 @@ func TibiaDataErrorHandler(c *gin.Context, err error, httpCode int) {
10871088
if httpCode == 0 {
10881089
if t.Code() == 10 || t.Code() == 11 {
10891090
httpCode = http.StatusInternalServerError
1090-
info.Status.HTTPCode = httpCode
10911091
} else {
10921092
httpCode = http.StatusBadRequest
1093-
info.Status.HTTPCode = httpCode
10941093
}
10951094
}
10961095

1096+
// An error occurred at tibia.com
1097+
if t.Code() > 20000 {
1098+
httpCode = http.StatusBadGateway
1099+
}
1100+
1101+
info.Status.HTTPCode = httpCode
10971102
info.Status.Error = t.Code()
10981103
info.Status.Message = t.Error()
10991104
case error:
@@ -1225,29 +1230,39 @@ func TibiaDataHTMLDataCollector(TibiaDataRequest TibiaDataRequestStruct) (string
12251230

12261231
if err != nil {
12271232
log.Printf("[error] TibiaDataHTMLDataCollector (Status: %s, URL: %s) in resp1: %s", res.Status(), res.Request.URL, err)
1233+
return "", err
1234+
}
12281235

1229-
switch res.StatusCode() {
1230-
case http.StatusForbidden:
1231-
// throttled request
1232-
LogMessage = "request throttled due to rate-limitation on tibia.com"
1233-
log.Printf("[warning] TibiaDataHTMLDataCollector: %s!", LogMessage)
1234-
return "", err
1235-
1236-
case http.StatusFound:
1237-
// Check if page is in maintenance mode
1238-
location, _ := res.RawResponse.Location()
1239-
if location != nil && location.Host == "maintenance.tibia.com" {
1240-
LogMessage := "maintenance mode detected on tibia.com"
1241-
log.Printf("[info] TibiaDataHTMLDataCollector: %s!", LogMessage)
1242-
return "", validation.ErrorMaintenanceMode
1243-
}
1244-
fallthrough
1245-
1246-
default:
1247-
LogMessage = "unknown error occurred on tibia.com"
1248-
log.Printf("[error] TibiaDataHTMLDataCollector: %s!", LogMessage)
1249-
return "", err
1236+
switch res.StatusCode() {
1237+
case http.StatusOK:
1238+
// ok request, nothing to be done
1239+
case http.StatusForbidden:
1240+
// throttled request
1241+
LogMessage = "request throttled due to rate-limitation on tibia.com"
1242+
log.Printf("[warning] TibiaDataHTMLDataCollector: %s!", LogMessage)
1243+
return "", validation.ErrStatusForbidden
1244+
case http.StatusFound:
1245+
// Check if page is in maintenance mode
1246+
location, _ := res.RawResponse.Location()
1247+
if location != nil && location.Host == "maintenance.tibia.com" {
1248+
LogMessage := "maintenance mode detected on tibia.com"
1249+
log.Printf("[info] TibiaDataHTMLDataCollector: %s!", LogMessage)
1250+
return "", validation.ErrorMaintenanceMode
12501251
}
1252+
1253+
LogMessage = fmt.Sprintf(
1254+
"unknown error occurred on tibia.com (Status: %d, RequestURL: %s)",
1255+
http.StatusFound, res.Request.URL,
1256+
)
1257+
log.Printf("[error] TibiaDataHTMLDataCollector: %s!", LogMessage)
1258+
return "", validation.ErrStatusFound
1259+
default:
1260+
LogMessage = fmt.Sprintf(
1261+
"unknown error and status occurred on tibia.com (Status: %d, RequestURL: %s)",
1262+
res.StatusCode(), res.Request.URL,
1263+
)
1264+
log.Printf("[error] TibiaDataHTMLDataCollector: %s!", LogMessage)
1265+
return "", validation.ErrStatusUnknown
12511266
}
12521267

12531268
if TibiaDataRequest.RawBody {

src/webserver_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,19 @@ func TestErrorHandler(t *testing.T) {
282282
c, _ = gin.CreateTestContext(w)
283283
TibiaDataErrorHandler(c, errors.New("test error"), 0)
284284
assert.Equal(http.StatusBadGateway, w.Code)
285+
286+
w = httptest.NewRecorder()
287+
c, _ = gin.CreateTestContext(w)
288+
TibiaDataErrorHandler(c, validation.ErrStatusForbidden, http.StatusForbidden)
289+
assert.Equal(http.StatusBadGateway, w.Code)
290+
291+
w = httptest.NewRecorder()
292+
c, _ = gin.CreateTestContext(w)
293+
TibiaDataErrorHandler(c, validation.ErrStatusFound, http.StatusFound)
294+
assert.Equal(http.StatusBadGateway, w.Code)
295+
296+
w = httptest.NewRecorder()
297+
c, _ = gin.CreateTestContext(w)
298+
TibiaDataErrorHandler(c, validation.ErrStatusUnknown, http.StatusConflict)
299+
assert.Equal(http.StatusBadGateway, w.Code)
285300
}

0 commit comments

Comments
 (0)