0
votes

I am aws lambda with Java to build one functionality and also integration response to handle custom mapping. here is code of lambda.

    @Override
    public LoginResponse handleRequest(LoginRequest request, Context ctx) {
        LambdaLogger logger = ctx.getLogger();

        LoginResponse response = new LoginResponse();
        if (StringUtils.isNullOrEmpty(request.getUsername())) {
            response.setErrorMessage("Invalid Username!!");
            return response;
        }

        String domainName = request.getUsername().substring(request.getUsername().indexOf('@') + 1);
        log.info("Domain name [{}] : ", domainName);
        Item itm = findDomainName(domainName);
        if(null == itm) {
            response.setErrorMessage("Invalid Username!!");
            throw new RuntimeException("Error Message");
        }
}

so here my understanding is, when system will throw RuntimeException then aws treat this as internal server error with status code 500 and it will execute template mapping thing. But its return always 200. How we can return 500 or some other status code so that it can mapped with integration response pattern using java.

Thanks

1
set the status to 500 before you throw your exception. For example for servlets the way to do it would be to add this line before you throw your exception response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR). - laserany
this response is my project local variable not http response. will it work or do you mean , autowired HttpServletResponse somehow and then set the code. - Still Learning
servlet was just an example you would not be using it here since you are using AWS lambda. Unfortunately I'm not an expert in AWS lambda but they must have some built in package that you can import to your code that would have a response variable that would allow you to change the status code before you throw your exception - laserany

1 Answers

0
votes

This issue has been resolved now and it was really simple fix.

Just throw any User defined or Runtime exception with error code.

if(null == itm) {
    throw new UserNotFoundException(ExceptionErrorCode.INTERNAL_SERVER_ERROR);
}

or

try {
    result = signIn(request.getUsername(), request.getPassword(), userPoolId, clientId);
}catch(NotAuthorizedException e) {
    throw new UserNotFoundException(ExceptionErrorCode.UN_AUTHORIZED);
}

and then mapped these error code in api Integration response. This that we need to do to send custom message/error code from lambda.

Thanks

enter image description here