Skip to content
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

Users want to customize status response when schema validation fails #841

Closed
jiachen1120 opened this issue Nov 24, 2020 · 8 comments
Closed
Assignees
Labels
enhancement Issue: Enhancement

Comments

@jiachen1120
Copy link
Contributor

jiachen1120 commented Nov 24, 2020

When schame validation fails, the framework will directly return the error status object {"statusCode":400 ...}. However, one of our user want to add their own information into this status response.

A generic way to implement it is to provide users a status wrapper interface and let user costomize their own Status and them inject the customized status into framework through SingletonFactory

example:

interface IStatusFactory {
    Status wrap(Status status, HttpServerExchange exchange);
}

User implement the IStatusFactory and configure service.yml

public class XXXXStatusFactory implements IStatusFactory {
   @Override
    public Status wrap(Status, HttpServerExchange exchange) {
        return XXXXStatus(status, exchange);
    }
}

public class XXXXStatus extends Status {
...
}
singletons:
    - com.networknt.status.IStatusFactory
        - com.networknt.petstore.model.XXXXStatusFactory

Within LightHttpHandler.setExchangeStatus() method:

default void setExchangeStatus(HttpServerExchange exchange, Status status) {
        // wrapping status into customized status (XXXXStatus)
        status = statusFactory == null ? status : statusFactory.wrap(status, exchange);
        ...
        exchange.getResponseSender().send(status.toString());
        ...
    }

Looking forward to your opinion. Thanks

@jiachen1120 jiachen1120 added the enhancement Issue: Enhancement label Nov 24, 2020
@stevehu
Copy link
Contributor

stevehu commented Nov 24, 2020

@jiachen1120 This is a good approach to customize the error message. To extend the concept of error message customization, one organization is asking if we can hide the detail of the status response to only the response code and error code for north-south traffic and keep the entire status response for east-west traffic. There is a security concern to send the full status object to the client coming from the Internet. If the Internet user receives the error, they don't know what is going on and they have to report to the API owner who can search the runbook to find the full text of descriptions as well as the resolutions in different scenarios.

@jiachen1120
Copy link
Contributor Author

@stevehu Thanks Steve. We also believe that it is good to provide users with customized error message. Therefore, can we make a pull request for this?

@stevehu
Copy link
Contributor

stevehu commented Nov 25, 2020

Yes. Please.

@miklish
Copy link
Collaborator

miklish commented Nov 30, 2020

@jiachen1120 One suggestion... The "I" in IStatusFactory does not align with the naming conventions in light-4j... I would suggest renaming this interface to simply StatusFactory... @stevehu what do you think?

@stevehu
Copy link
Contributor

stevehu commented Nov 30, 2020

@jiachen1120 I think @miklish is right. It is seldom for us to use I as a prefix these days as the modern IDE has a clear indicator on which file is a class and which one is an interface. The prefix will make the interface name not a valid word and causing some warnings in some IDEs. The practice was pretty popular in the old days that IDE doesn't have any indication on the class type. Thanks.

@jiachen1120
Copy link
Contributor Author

@miklish @stevehu I see, will change it. Thanks for the explaination!

jiachen1120 added a commit that referenced this issue Dec 10, 2020
- added StatusWrapper interface
stevehu pushed a commit that referenced this issue Dec 14, 2020
- added StatusWrapper interface
@stevehu
Copy link
Contributor

stevehu commented Dec 14, 2020

stevehu pushed a commit that referenced this issue Dec 14, 2020
- added StatusWrapper interface
@stevehu
Copy link
Contributor

stevehu commented Dec 14, 2020

@stevehu stevehu closed this as completed Dec 15, 2020
younggwon1 pushed a commit to younggwon1/light-4j that referenced this issue Feb 10, 2024
- added StatusWrapper interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Issue: Enhancement
Projects
None yet
Development

No branches or pull requests

3 participants