Skip to content

Fresh built handlers2 error #3804

@FredslundMagnus

Description

@FredslundMagnus

I am not sure what this bug is I get it a few times pr. hour on random times, it changed in 2.3.0 so I am providing information both for before and after 2.3.0 in case that helps. In only happens in production so I can only provide the code from after the build step.

Fresh <2.3.0:

TypeError: Cannot read properties of undefined (reading 'length')
    at UrlPatternRouter.match (file:///.../_fresh/server/server-entry.mjs:5150:21)
    at file:///.../_fresh/server/server-entry.mjs:6036:30
    at mapped (ext:deno_http/00_serve.ts:360:24)
    at mapped (ext:deno_http/00_serve.ts:457:16)
    at ext:deno_http/00_serve.ts:750:29
if (staticMatch !== void 0) {
  result.pattern = url2.pathname;
  let handlers2 = staticMatch.byMethod[method];
  if (method === "HEAD" && handlers2.length === 0) {
    handlers2 = staticMatch.byMethod.GET;
  }
  if (handlers2 == undefined) {
    console.log({ method, pathname: url2.pathname }); // I added this
  }
  if (handlers2.length > 0) {
    result.methodMatch = true;
    result.handlers.push(...handlers2);
  }
  return result;
}

I added a console log and got:

{
  "method": "PROPFIND",
  "pathname": "/"
}

Fresh <=2.3.0:

TypeError: handler2 is not a function
    at file:///.../_fresh/server/server-entry.mjs:6478:51
    at mapped (ext:deno_http/00_serve.ts:386:24)
    at mapped (ext:deno_http/00_serve.ts:489:16)
    at ext:deno_http/00_serve.ts:782:2
handler() {
    let buildCache = __privateGet(this, _getBuildCache).call(this);
    if (buildCache === null) {
      if (this.config.mode === "production" && DENO_DEPLOYMENT_ID !== void 0) ;
      else {
        buildCache = new MockBuildCache([], this.config.mode);
      }
    }
    const router = new UrlPatternRouter();
    const {
      rootHandler
    } = applyCommands(router, __privateGet(this, _commands), this.config.basePath, __privateGet(this, _onError));
    const trustProxy = this.config.trustProxy;
    return async (req, conn = DEFAULT_CONN_INFO) => {
      const url2 = new URL(req.url);
      url2.pathname = url2.pathname.replace(/\/+/g, "/");
      if (trustProxy) {
        const proto = req.headers.get("x-forwarded-proto");
        if (proto) {
          url2.protocol = proto + ":";
        }
        const host = req.headers.get("x-forwarded-host");
        if (host) {
          url2.host = host;
        }
      }
      const method = req.method.toUpperCase();
      const matched = router.match(method, url2);
      let {
        params,
        pattern,
        item: handler2,
        methodMatch
      } = matched;
      const span = _trace.getActiveSpan();
      if (span && pattern) {
        span.updateName(`${method} ${pattern}`);
        span.setAttribute("http.route", pattern);
      }
      let next2;
      if (pattern === null || !methodMatch) {
        handler2 = rootHandler;
      }
      if (matched.pattern !== null && !methodMatch) {
        if (method === "OPTIONS") {
          const allowed = router.getAllowedMethods(matched.pattern);
          next2 = defaultOptionsHandler(allowed);
        } else {
          next2 = DEFAULT_NOT_ALLOWED_METHOD;
        }
      } else {
        next2 = DEFAULT_NOT_FOUND;
      }
      const ctx = new Context(req, url2, conn, matched.pattern, params, this.config, next2, buildCache);
      try {
        const result = await (handler2 !== null ? handler2(ctx) : next2()); // This line
        if (!(result instanceof Response)) {
          throw new Error(`Expected a "Response" instance to be returned, but got: ${result}`);
        }
        if (method === "HEAD") {
          return new Response(null, result);
        }
        return result;
      } catch (err) {
        ctx.error = err;
        return await DEFAULT_ERROR_HANDLER(ctx);
      }
    };
  }

The error happened at the // This line comment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions