Skip to content

Better json error response #260

Closed
Closed

Description

@wentzlau

I think that the error responses needs to be more structured.
Right now an error response looks something like this when creating a user:
{"message":"Invalid password: Password must be between 6 and 32 chars and contain ...","code":400, ...

I will have to parse response message to figure out the error. If the message is translated it makes it even more difficult.

would like something like this
{
"errors":{
[
"type": "parameter",
"name": "password",
"message": "Invalid password: Password must be between 6 and 32 chars and contain..."
]
...
}

such an error is very easy to handle and put into the UI.

Activity

  1. eldadfux commented on Nov 22, 2019

    @eldadfux
    Member

    @wentzlau yeh, this is definitely an area where we can do better, I would really appreciate if you try and take a look at the way we handle error messages today and help me think how we can manage this better without creating a huge overhead.

    Please keep in mind that there are some cases where we are intentionally giving general errors in favor of security.

    This is our current error handler:
    https://github.com/appwrite/appwrite/blob/master/app/app.php#L267

    And all user input validation is handled directly at the framework level:
    https://github.com/utopia-php/framework/blob/master/src/App.php#L440
    (Utopia framework is maintained by the Appwrite team)

    This is an example controller:
    https://github.com/appwrite/appwrite/blob/master/app/controllers/storage.php#L116

  2. wentzlau commented on Nov 22, 2019

    @wentzlau
    Author

    I think that you should handle validation errors differently from exceptions. Right now you handle validation errors as exceptions and you are only able to catch one validation error per post to the api. All validation errors should be found in a request and in case of error raise a validation exception that holds all errors.

    A rule of thumb is that if a user can move forward by changing the input it is a validation error. The validation error message should explain to the user what should be fixed. If the system fails in a way that prevent the user from moving forward in the UI flow it is an exception often such errors/exceptions requires technical support.

    If you raise a validation exception it could be caught in the api frameworks for appwrite.

    in python a call to appwrite could look like this

    user = User(client)
    try:
        user.create_user(email, password, name)
    except ValidationException ex:
        for validation_error in ex.validation_errors:
            #prepare response to UI with validation errors          
    except Exception ex:
        #Something went wrong log the error and prepare recovery in UI  
        log.exception(ex)
  3. eldadfux commented on Nov 22, 2019

    @eldadfux
    Member

    Using error stacks instead of throwing an exception might be a good option as long as it doesn't effect the server in any other way. It might be a really big change so I'll have to do a farther research in that area and I guess that for now this is not a top priority.

    The current implementation allows a developer to distinguish input error from other errors by using the HTTP status code. Appwrite returns 400 status codes for every bad input (or "bad request").
    More about Appwrite errors can be found here: https://appwrite.io/docs/error-codes

    Even if we'll change the validation method this still won't solve the problem where some errors we throw might be better described. I guess the most effective way to improve this will be to manually review each error.

    Another idea I had is to allow error localisation and that way we will allow developers to just show users the errors directly from the API response.

  4. added
    discussion
    enhancement
    on Nov 23, 2019
  5. wentzlau commented on Nov 24, 2019

    @wentzlau
    Author

    A solution would be to include an error code in the current error response. You could prepend the error code with error type like "Fatal.SMTPServiceNotFound", "Validation.InvalidPassword" or "Authentication.UnknownUserOrPassword"

    something like:
    { "error": "Validation.InvalidPassword", "message":"Invalid password: Password must be between 6 and 32 chars and contain ...","code":400, ...

    In the appwrite code

    if (!$validator->isValid($value)) {
        throw new Exception($validator->getErrorCode(),  'Invalid ' .$key . ': ' . $validator->getDescription(), 400);
    }

    $validator->getErrorCode() returns "Validation.InvalidPassword"

  6. self-assigned this
    on Nov 29, 2019
  7. TorstenDittmann commented on Apr 6, 2021

    @TorstenDittmann
    Contributor
  8. eldadfux commented on Apr 10, 2021

    @eldadfux
    Member

    We have already released this feature on some SDKs (Node, Dart), should be available on all SDKs alongside the release of Appwrite 0.8

  9. added a commit that references this issue on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

discussionenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions