|
4 | 4 | "bytes" |
5 | 5 | "encoding/json" |
6 | 6 | "errors" |
| 7 | + "fmt" |
7 | 8 | "log" |
8 | 9 | "net/http" |
9 | 10 | "os" |
@@ -1087,13 +1088,17 @@ func TibiaDataErrorHandler(c *gin.Context, err error, httpCode int) { |
1087 | 1088 | if httpCode == 0 { |
1088 | 1089 | if t.Code() == 10 || t.Code() == 11 { |
1089 | 1090 | httpCode = http.StatusInternalServerError |
1090 | | - info.Status.HTTPCode = httpCode |
1091 | 1091 | } else { |
1092 | 1092 | httpCode = http.StatusBadRequest |
1093 | | - info.Status.HTTPCode = httpCode |
1094 | 1093 | } |
1095 | 1094 | } |
1096 | 1095 |
|
| 1096 | + // An error occurred at tibia.com |
| 1097 | + if t.Code() > 20000 { |
| 1098 | + httpCode = http.StatusBadGateway |
| 1099 | + } |
| 1100 | + |
| 1101 | + info.Status.HTTPCode = httpCode |
1097 | 1102 | info.Status.Error = t.Code() |
1098 | 1103 | info.Status.Message = t.Error() |
1099 | 1104 | case error: |
@@ -1225,29 +1230,39 @@ func TibiaDataHTMLDataCollector(TibiaDataRequest TibiaDataRequestStruct) (string |
1225 | 1230 |
|
1226 | 1231 | if err != nil { |
1227 | 1232 | log.Printf("[error] TibiaDataHTMLDataCollector (Status: %s, URL: %s) in resp1: %s", res.Status(), res.Request.URL, err) |
| 1233 | + return "", err |
| 1234 | + } |
1228 | 1235 |
|
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 |
1250 | 1251 | } |
| 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 |
1251 | 1266 | } |
1252 | 1267 |
|
1253 | 1268 | if TibiaDataRequest.RawBody { |
|
0 commit comments