winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.06k stars 198 forks source link

Provide users the error when Json.tryParse fails #7114

Open boyney123 opened 2 months ago

boyney123 commented 2 months ago

Use Case

I'm using Json.tryParse to well.... try and parse some Json 😅, but it's failing. It would be nice to capture this and log it out if I choose too.

Here is my example code.

if let payload = Json.tryParse(req.body) {
    let email = payload.get("email").asStr();
    let friend:types.Friend = { id: util.uuidv4(), createdAt: datetime.utcNow().toIso(), email: email };
    spaceTable.addFriend(req.vars.get("spaceId"), friend);  
    return cloud.ApiResponse {
      body: Json.stringify(friend)
    };
  } else {
    return cloud.ApiResponse {
      status: 500,
      body: "Failed to process request",
    };
  }

My API returns a 500 (regardless of the issue), but it would be nice to capture the error and log somehow...

Proposed Solution

Maybe payload could have a error on it? I'm not really sure what I would expect here....

Implementation Notes

No response

Component

No response

Community Notes