-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error handling #86
error handling #86
Conversation
Codecov Report
@@ Coverage Diff @@
## feature-error-handling #86 +/- ##
==========================================================
+ Coverage 79.41% 80.59% +1.18%
==========================================================
Files 21 21
Lines 2647 2669 +22
==========================================================
+ Hits 2102 2151 +49
+ Misses 474 451 -23
+ Partials 71 67 -4 |
7f2454d
to
4fe97c4
Compare
As discussed in the discord server, some http codes should be updated.
|
f08fd5d
to
af99cc0
Compare
Kudos, SonarCloud Quality Gate passed! |
* add tibia mapping * create the static package * create the validation package * implement error handling
* add tibia mapping * create the static package * create the validation package * implement error handling
* add tibia mapping * create the static package * create the validation package * implement error handling
* add tibia mapping * create the static package * create the validation package * implement error handling
* add tibia mapping * create the static package * create the validation package * implement error handling
* error handling (#86) * add tibia mapping * create the static package * create the validation package * implement error handling * adding response codes for failures even if the codes from Kong are missing too.. * adding codecov proxy support due to failure of tests being rate-limited * adjusting log message with URL * fixing docker build issue adding modules for every module inside the src folder adjusting Dockerfile order of copy and build step updating go mod and workflows to go version 1.18 * removing moved testdata files due to not being deleted when rebased * adjusting some more code things Co-authored-by: Pedro Pessoa <pedro_santos_40@hotmail.com>
Ok, this is a massive PR and also a rewrite of good chunks of the code. I splitted it into a few commits to make it easier to follow. I'll also try to summarize it as much as I can here.
Reasoning: the webserver was already running on prod and was panicking on pretty much every error, which is not good.
Besides that, invalid inputs (for example: 123456 as a char name) would return an "empty" json instead of an error.
Changes:
1: Static package
I created a static package and moved all test files inside it (without any changes to the test files). The reason was to be able to embed the files. Since I created "nested" packages, this helps a lot with dealing with file paths.
2: Tibia mapping package
Created a separate package to fetch data from assets.tibiadata.com. This was mainly to organize the code.
3: Validation package
This is one of the biggest changes. This is where all the validation is happening. I even moved some validation that was being handled in the main package into here.
This is how this package works: I implemented a bunch of error codes (errors.go), this makes it easier to debug code and also give users a more reasonable error response when making a request.
I then made all handlers funcs validate their input (when needed), this reduces the amount of API calls made to tibia.com, but, most importantly, it gives us great control as to what error to return to the user. For example: instead of only responding with "character not found" we can give the user an error that exists in the input (name too small or too big for example).
This package also has the limits.go file which are constants from tibia.com
4: Env Vars funcs logic fixThis is a small change, isEnvExist and getEnv were simplified and had their logics fixed.(#91)5: Consistency changesI changed all Tibiadata names to TibiaData, just for consistency.(#90)Stoped using ioutil as it is deprecated.(#92)6: Debug endpoint
I created a /debug endpoint that shows some useful info about the validation package.
7: Error Handling
This is the biggest change on this PR. Here's what was done:
All "Impl" funcs now return a pointer to their respective response and an error. This is how go handle errors. Before this, we were panicking on every error. Now we just return the error to the user.
I changed a few ".Each" funcs to ".EachWithBreak", this is to be able to break the loop if an error occurs.
Character not found, creature not found, spell not found, etc are now properly handled.
Added a Status struct to the Information struct, this is returned on every request for consistency and contains these fields: HTTPCode, Error, Message.
Error is the int value of the error, and Message is a readable message from the error.
Created the TibiaDataErrorHandler that should be used everytime we want to return an error.
TibiaDataAPIHandleResponse does not handle errors anymore and should not be called in case of an error.