# Errors Taquito (https://github.com/ecadlabs/taquito/issues/160)
## Plan to improve the error pattern in Taquito
- Task 1: Create high-level categories of errors that our existing errors will extend. It will help users to identify what type of issue they are facing. These errors will be defined in a new package, `@taquito/core`. (An idea unrelated to the current scope will be to move some of our interfaces in the `@taquito/core` package over time to remove the circular dependencies between the packages.) The categories of errors can be:
- ParameterValidationError: errors we use when handling incoming user data
- RpcError: errors returned by the RPC (RPC didn't accept an operation, ...)
- TezosToolkitConfigError: something has not been appropriately configured on the TezosToolkit
- UnsupportedAction: user tries to do something that is not supported by Taquito (this category of error should be reported to us)
- NetworkError
- PermissionDeniedError
- Task 2: Consolidate duplicated Errors accross packages (InvalidViewParameterError, InvalidAddressError, InvalidBlockHashError, InvalidHexStringError, InvalidMessageError, ...)
- Task 3 (should be split in one PR per package): Go through all the errors in each package and:
- Make the error extends one of the categories above
- Ensure that the error message is informative (what went wrong?) Use complete but simple sentences
- When there is a problem with a value received as a parameter, the erroneous value should be part of the error message and an indication of what is wrong with it. For example: `The key "inva2obfVMEuPUnadAConLWk7Tf4Dt3n4svSgJwrgpamRqJXvaYcg1" is invalid. It contains the invalid prefix "inva", but "p2sk" was expected.` Surround the variable in the error message with quotes for clarity.
- Make sure that the variables are displayed properly in the error message to avoid error messages like: `Failed to encode value '[object Object]'`. Add tests to ensure proper rendering.
- Ensure that the errors are actionable (tell the users what they can do to resolve the issue)
- If an error overrides another error, make sure that it includes the "original error" as a param.
- Document the error with a typedoc comment
- Add an error code to each error with an additional explanation on a documentation page of the Taquito website. Add a link to the documentation in error.
- Collect all error classes and put it inside a single-consistently named file in each package. E.g. an `errors.ts` file in the root of each package's /src folder. It will increase the ease of extending and maintaining error classes in the future.
- When a method/function can throw an error, include the tag [@throws](https://typedoc.org/tags/throws/) in the typedoc comment with an explanation of when the error can be thrown.
- Task 4: @taquito/michelson-encoder package - We use the same error if an issue happens in the Encode and Execute methods. In some situations, it is hard to identify what went wrong. Make sure we clearly distinguish if it is a bad input from the user or an issue from the Execute method.
- Task 5: Pay specific attention to the error returned by the RPC. Sometimes, the error is vague and not informative enough. We should provide a custom error with additional information in those cases. TODO: use Taquito in different ways to generate failure from the RPC, inspect the error coming from the RPC, identify error messages that would benefit from clarification, and make the appropriate changes. Here are two examples that need to be addressed:
- When trying to fetch a big map value using a key of the wrong type (for example, using X where a nat was expected), an error from the node bubble up and is hard to diagnose:
```
Uncaught (in promise) HttpResponse: Http error response: (400) Failed to parse the request body: No case matched:
Unhandled error (Invalid_argument "Z.of_substring_base: invalid digit")
Missing object field string
Missing object field bytes
Unexpected object instead of array
Missing object field prim
at HttpBackend.<anonymous> (http://localhost:3030/node_modules/.vite/deps/chunk-V4G7R2DB.js?v=4243d8f3:1552:17)
at Generator.throw (<anonymous>)
at rejected (http://localhost:3030/node_modules/.vite/deps/chunk-V4G7R2DB.js?v=4243d8f3:1390:32)
```
- An error comes from the RPC when estimating an operation using an account with a balance too low (https://github.com/ecadlabs/taquito/issues/363). TODO: validate if the behavior is still the same; the issue was opened in 2020.
```
HttpResponseError {
message: 'Http error response: (400) Failed to parse the request body: No case matched:\n' +
' At /kind, unexpected string instead of endorsement\n' +
' At /kind, unexpected string instead of seed_nonce_revelation\n' +
' At /kind, unexpected string instead of double_endorsement_evidence\n' +
' At /kind, unexpected string instead of double_baking_evidence\n' +
' At /kind, unexpected string instead of activate_account\n' +
' At /kind, unexpected string instead of proposals\n' +
' At /kind, unexpected string instead of ballot\n' +
' At /kind, unexpected string instead of reveal\n' +
' At /destination:\n' +
' Unhandled error (Failure "Invalid contract notation.")\n' +
' At /kind, unexpected string instead of origination\n' +
' At /kind, unexpected string instead of delegation',
status: 400,
statusText: 'Bad Request',
body: 'Failed to parse the request body: No case matched:\n' +
' At /kind, unexpected string instead of endorsement\n' +
' At /kind, unexpected string instead of seed_nonce_revelation\n' +
' At /kind, unexpected string instead of double_endorsement_evidence\n' +
' At /kind, unexpected string instead of double_baking_evidence\n' +
' At /kind, unexpected string instead of activate_account\n' +
' At /kind, unexpected string instead of proposals\n' +
' At /kind, unexpected string instead of ballot\n' +
' At /kind, unexpected string instead of reveal\n' +
' At /destination:\n' +
' Unhandled error (Failure "Invalid contract notation.")\n' +
' At /kind, unexpected string instead of origination\n' +
' At /kind, unexpected string instead of delegation',
name: 'HttpResponse'
}
```
Some specific issues to fix with the current errors:
- When querying a big map with Taquito, if the key is not passed properly, an Error is thrown saying Error: Unable to encode big map key; We need to provide more information on why Taquito is unable to encode it.