Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Still getting runtime error: There is already a Construct with name '$Closure1' #5063

Closed
skyrpex opened this issue Nov 27, 2023 · 2 comments
Closed
Labels
🐛 bug Something isn't working 🐶 dogfood Discovered while dogfooding Winglang 🎨 sdk SDK

Comments

@skyrpex
Copy link
Contributor

skyrpex commented Nov 27, 2023

I tried this:

bring cloud;

struct JsonApiProps {
  api: cloud.Api;
}

struct JsonApiResponse {
  status: num?;
  headers: Map<str>?;
  body: Json?;
}

pub class JsonApi {
  api: cloud.Api;
  pub url: str;

  new(props: JsonApiProps) {
    this.api = props.api;
    this.url = this.api.url;
  }

  wrapHandler(handler: inflight (cloud.ApiRequest): JsonApiResponse): inflight (cloud.ApiRequest): cloud.ApiResponse {
    return inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
      try {
        let response = handler(request);

        let headers = response.headers?.copyMut();
        headers?.set("content-type", "application/json");

        let var bodyStr = "";
        if let body = response.body {
          bodyStr = Json.stringify(body);
        }

        return {
          status: response.status ?? 200,
          headers: headers?.copy(),
          body: bodyStr,
        };
      } catch error {
        // TODO: This is a hack to get around the fact that errors are just strings
        if error == "Bad credentials" {
          return {
            status: 401,
            headers: {
              "content-type": "application/json",
            },
            body: Json.stringify({
              error: error,
            }),
          };
        }

        return {
          status: 500,
          headers: {
            "content-type": "application/json",
          },
          body: Json.stringify({
            error: error,
          }),
        };
      }
    };
  }

  pub get(path: str, handler: inflight (cloud.ApiRequest): JsonApiResponse) {
    this.api.get(path, this.wrapHandler(handler));
  }
}

let jsonApi = new JsonApi(api: new cloud.Api());
jsonApi.get("/a", inflight () => {
  return {
    body: { path: "/a"},
  };
});
jsonApi.get("/b", inflight () => {
  return {
    body: { path: "/b"},
  };
});

This happened:

runtime error: There is already a Construct with name '$Closure1' in JsonApi [JsonApi]

I expected this:

After #4993 got merged, I'd assume that we wouldn't get this error again.

Is there a workaround?

Yes, doing an internal counter for preflights:

bring cloud;

struct JsonApiProps {
  api: cloud.Api;
}

struct JsonApiResponse {
  status: num?;
  headers: Map<str>?;
  body: Json?;
}

pub class JsonApi {
  api: cloud.Api;
  pub url: str;
  var handlerCount: num;

  new(props: JsonApiProps) {
    this.api = props.api;
    this.url = this.api.url;
    this.handlerCount = 0;
  }

  wrapHandler(handler: inflight (cloud.ApiRequest): JsonApiResponse): inflight (cloud.ApiRequest): cloud.ApiResponse {
    class MyHandler {
      inflight handle(request: cloud.ApiRequest): cloud.ApiResponse {
        try {
          let response = handler(request);

          let headers = response.headers?.copyMut();
          headers?.set("content-type", "application/json");

          let var bodyStr = "";
          if let body = response.body {
            bodyStr = Json.stringify(body);
          }

          return {
            status: response.status ?? 200,
            headers: headers?.copy(),
            body: bodyStr,
          };
        } catch error {
          // TODO: This is a hack to get around the fact that errors are just strings
          if error == "Bad credentials" {
            return {
              status: 401,
              headers: {
                "content-type": "application/json",
              },
              body: Json.stringify({
                error: error,
              }),
            };
          }

          return {
            status: 500,
            headers: {
              "content-type": "application/json",
            },
            body: Json.stringify({
              error: error,
            }),
          };
        }
      }
    }
    this.handlerCount += 1;
    return new MyHandler() as "Handler{this.handlerCount}";
  }

  pub get(path: str, handler: inflight (cloud.ApiRequest): JsonApiResponse) {
    this.api.get(path, this.wrapHandler(handler));
  }
}

Anything else?

No response

Wing Version

0.50.6

Node.js Version

No response

Platform(s)

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
@skyrpex skyrpex added the 🐛 bug Something isn't working label Nov 27, 2023
@monadabot monadabot added this to Wing Nov 27, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New - not properly defined in Wing Nov 27, 2023
@skyrpex skyrpex added 🎨 sdk SDK 🐶 dogfood Discovered while dogfooding Winglang labels Nov 27, 2023
@staycoolcall911 staycoolcall911 moved this from 🆕 New - not properly defined to 🤝 Backlog - handoff to owners in Wing Nov 28, 2023
@skorfmann
Copy link
Contributor

here's another example of where this is troublesome simulator - essentially adding inflight functions programmatically is broken.

would be really nice if there was a proper solution to this

@staycoolcall911
Copy link
Contributor

Duplicates #4925

@staycoolcall911 staycoolcall911 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2024
@github-project-automation github-project-automation bot moved this from 🤝 Backlog - handoff to owners to ✅ Done in Wing Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🐶 dogfood Discovered while dogfooding Winglang 🎨 sdk SDK
Projects
Archived in project
Development

No branches or pull requests

3 participants