1

I'm seeing a lot of examples for node.js to fail a lambda using context.fail(response).

What is the c# equivalent of context.fail(response) since the ILambdaContext for c# doesn't have fail or succeed methods on it?

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • 1
    I think the C# equivalent is to throw an exception. No exception == success. – Matt Houser May 23 '17 at 15:26
  • A bit of a correction: the `context.fail(error)` is used for the [old node.js api](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html#nodejs-prog-model-oldruntime-context-methods). The [new node.js api](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html) uses a callback model. Now to answer the question: [the documentation](http://docs.aws.amazon.com/lambda/latest/dg/dotnet-exceptions.html) states how to handle function errors in C#. – Dustin Kingen May 23 '17 at 15:33
  • Throwing an exception worked, I needed to tie in the regex on the integration response, but I was able to get it to 500, thanks! – RandomUs1r May 23 '17 at 15:40

1 Answers1

1

In the Node.js version 0.10.42 runtime the context contained methods for succeed, fail, and done[1]. This model was replaced with a callback model which removed the methods from the context and errors are passed from a callback[2].

In the C# programming model the analogous is to throw an exception[3].

From the docs:

When an exception occurs in your Lambda function, Lambda will report the exception information back to you. Exceptions can occur in two different places:

  • Initialization (Lambda loading your code, validating the handler string, and creating an instance of your class if it is non-static).
  • The Lambda function invocation.

An example is given where an exception is thrown from a Handler:

namespace Example {            
   public class AccountAlreadyExistsException : Exception {
      public AccountAlreadyExistsException(String message) :
         base(message) {
      }
   }
} 

namespace Example {
   public class Handler {
     public static void CreateAccount() {
       throw new AccountAlreadyExistsException("Account is in use!");
     }
   }
}

[1]: Using Earlier Node.js Runtime (v0.10.42)

[2]: Function Errors (Node.js)

[3]: Function Errors (C#)

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • 2
    There's a little bit more involved here than throwing an exception, I also needed to create a method response status code and an integration response with a regex linking to the status code. – RandomUs1r May 23 '17 at 16:15
  • @RandomUs1r Hi can you please provide a detailed answer with code. I've been struggling to get proper error codes from C# lambda integrated with API gateway. Please see this question - https://stackoverflow.com/questions/72085211/how-to-get-proper-http-status-codes-from-amazon-api-gateway-integrated-with-c-sh – troglodyte07 May 05 '22 at 14:22