Lambda Error Handling
Goal
Build a robust error-handling layer for your SEC Lambda functions so that failures surface clearly in logs, callers receive structured error responses, and you can diagnose problems without redeploying.
Contract
Your Lambda function must:
Return a structured JSON error response (not a stack trace) when something goes wrong.
Distinguish between client errors (bad input) and server errors (downstream failures).
Produce CloudWatch log entries that let you trace a specific invocation from request to failure.
Set the appropriate HTTP status code when invoked behind API Gateway (via the Lambda contract).
When another Lambda invokes yours synchronously, the caller must detect failures via the FunctionError field in the invoke response — not by parsing the payload alone.
Required Reading
Constraints
Use
try/except(Python's term — not "try/catch").Every
exceptblock must log the exception with enough context to identify the failing invocation (request ID, input parameters).Never return a raw traceback to callers. Return a JSON body with an
errorkey describing the failure class.When invoking another Lambda with
boto3, always check the response for theFunctionErrorkey before reading the payload.Use
sam logsandsam local invokefor local reproduction before deploying diagnostic changes.All model references link to the Models reference page.
Acceptance Criteria
Your handler wraps all business logic in a
try/exceptthat catches specific exception types before a final catch-allException.A Bedrock
invoke_modelcall that fails (throttle, invalid input, expired model) is caught, logged with the request context, and returned as a structured error.An SEC EDGAR HTTP request that fails (network timeout, 403 from missing User-Agent, non-200 status) is caught and surfaced with the URL that failed.
When Lambda A invokes Lambda B synchronously, Lambda A checks
response["FunctionError"]and handles the downstream failure without crashing.You can find your Lambda's log group in CloudWatch, locate a specific invocation by request ID, and read the error output.
You can reproduce a failure locally using
sam local invokewith a test event, and stream deployed logs usingsam logs --tail.
Hints
Last verified: 2026-06
Last updated