0
votes

I am learning about AWS Lambda for a project I'm working on in Java. One of the requirements is that we return most errors using our custom response type (rather than using the default AWS Lambda error handling), but I'm wondering if it'd be possible to throw an exception as well after writing to the output stream. Seems weird to me, but this is what I have so far:

@Override
public void handleRequest(final InputStream inputStream, 
    final OutputStream outputStream, 
    final Context context) {

  ABCRequest request;
  ABCResponse response;

  try {
      request = this.mapper.readValue(requestStream, ABCRequest.class);
  } catch (final IOException e) {
      response = createResponse(..."error", etc);
      writeResponseToOutput(response, outputStream); //writes the response to the output stream
      throw new SomeCustomException("...", e);
  }

  response = handle(request);
  writeResponseToOutput(response, outputStream);
}

All I'm trying to do is to catch any deserialization issues from reading the value out of the request, and respond with that error response if there's an exception there. Otherwise continue and handle the request and get a "normal" response. But it seems a little weird / messy to me.

Any suggestions on how to better handle this situation?

For the caller calling the Lambda, do they only get the response output we sent? Or do they also get the default Lambda output thrown into the stream (from the exception)? (We just want the exception to show up in logs, not be returned to the client.)

1

1 Answers

0
votes

Based on this documentation, it seems like the recommended approach for these is to include details into error payload, and include that as part of the thrown exception.

That way, the exception is still thrown, and Lambda can properly label the response as an error instead of having to deal with the success / failure indicators manually.