1

I'm trying to return status error codes (ex. 500, 400, 401) through my AWS Gateway and can't seem to figure it out.

I've seen a few examples, where they do something like:

return context.fail('Bad Request: You submitted invalid input');

However, there is no fail or succeed methods on the ILambdaContext object.

Here's what I've got so far (non-working)

Lambda Code:

 var addressresponse = new AddressValidationResponse();
            addressresponse.Errors = new string[1];
            addressresponse.Errors[0] = "test error";
            addressresponse.Reason = "client_error";

 return addressresponse;

Custom Error model

Integration Response

Method Response

I think I'm close, but I'm still getting a 200 back with the response of:

{
  "Reason": "client_error",
  "Errors": [
    "test error"
  ]
}

What am I missing here?

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44

3 Answers3

1

You need throw an exception in your C# lambda function and use the API gateway console to map a specific error code to the HTTP response code in the response mapping templates.

Yeshodhan Kulkarni
  • 2,844
  • 1
  • 16
  • 19
  • I thought I tried that following http://marcelog.github.io/articles/aws_api_gateway_return_custom_http_status_codes.html . No luck though, I posted a bunch of screen shots in the hopes that somebody might see something I'm not seeing. – RandomUs1r May 19 '17 at 20:46
  • Yep, this + throwing an exception in c# lets me set the status code. Add the exception part, let me know and I'll accept. – RandomUs1r May 24 '17 at 17:11
0

You can use the APIGatewayHttpApiV2ProxyResponse to wrap your response, like below:

public static async Task<APIGatewayHttpApiV2ProxyResponse?> FunctionHandler(APIGatewayHttpApiV2ProxyRequest request, ILambdaContext context)
{
    var response = new APIGatewayHttpApiV2ProxyResponse();
    try
    {
        response.Body = await DoSomeWorkAsync();
        response.StatusCode = (int)HttpStatusCode.OK;
    }
    catch (Exception ex)
    {
        response.StatusCode = (int)HttpStatusCode.InternalServerError;
        response.Body = JsonSerializer.Serialize(new { error = ex.Message }) ;
    }

    return response;
}
Liam
  • 5,033
  • 2
  • 30
  • 39
-1

I answered something similar here

Basically you need to return a response object that APIG recognises.

There's C# example here

I would say I don't C# in the context Lambda though, basically you need to return headers, statusCode and response Body.

Community
  • 1
  • 1
Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • That makes sense, I copy pasted the code (and added error text) from the c# example and commented mine out, however I still get a 200 from the lambda with the error body. I set up a regex to check for error.*, but no luck. Do I need to specify any mapping templates or custom headers to get the Response object (from the link) to work with the gateway? – RandomUs1r May 23 '17 at 14:51
  • This is incorrect, in c# an exception needs to be thrown that ties into an integration response regex. – RandomUs1r May 24 '17 at 17:10
  • Aha there you go! – Mrk Fldig May 24 '17 at 17:27