I'm using Serverless to create Python AWS Lambda functions triggered by httpApi events.
My function (yes, I'm just passing back the context passed in by API Gateway so I can look at it):
def get_context(event, context):
try:
typ = str(type(context))
body = {
"context_type": typ,
"context": context
}
return {"statusCode": 200, "body": json.dumps(body)}
except Exception as e:
estr = f"other python error: {e}"
logger.error(estr) # AWS CloudWatch
body = {"message": "FAIL", "error": estr}
return {"statusCode": 500, "body": body}
I know the error in the try block is that the context object isn't serializable (thanks to the AWS Console).
But this question is about how the Exception block isn't making it to what Axios shows me client-side.
When I test it in the AWS Console, I get what I'd expect: Screenshot AWS Console shows my message
But when I use Axios, all I get is an Axios Error with error.response.status = 500 and error.response.data.message = "Internal Server Error".
I've spent hours searching stackoverflow, the serverless docs, etc. This pattern of returning a statusCode and a body works elsewhere. I can't figure out why I see my body in the AWS console and when I invoke via Serverless, but not in the Axios response. Any ideas? If it helps, I'm calling it in this function:
async apiPost(apiPath) {
// apiPath = "/public/test/context"
const apiName = "{resource defined in Amplify Configuration}";
const myInit = {
headers: {}, // OPTIONAL
response: true, // OPTIONAL (return the entire Axios response object instead of only response.data)
};
try {
const res = await API.post(apiName, apiPath, myInit); // aws-amplify API (Axios)
this.api_res = JSON.stringify(res, null, 2);
this.api_res_type = "Response";
} catch (e) {
this.api_res = axiosErrorHandler(e); // Just my attempts to dig through the error object...
this.api_res_type = "ERROR";
}
},
I've tried various combinations of json.dumps Python side, and iterating over the Axios properties clientside, trying to dig and see if it was getting buried somewhere on the Axios object, etc.
What I'd expect is for the Axios response data to have the body I sent to it, instead of a generic error masking what's really going on.
For the curious, here's the handler function. It just proves my response text isn't in the response anywhere / isn't related to why it isn't:
function axiosErrorHandler(err) {
let res = {};
if (err.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
res["condition"] = "Server Responded";
res["body"] = err.response.data;
res["statusCode"] = err.response.status;
res["headers"] = err.response.headers;
} else if (err.request) {
// The request was made but no response was received
// `err.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
res["condition"] = "No Server Response";
res["request"] = err.request;
} else {
// Something happened in setting up the request that triggered an Error
res["condition"] = "Request Setup Error";
res["message"] = err.message;
}
return JSON.stringify(res, null, 2);
}