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

catch async errors #347

Merged
merged 5 commits into from
Oct 15, 2022
Merged

catch async errors #347

merged 5 commits into from
Oct 15, 2022

Conversation

cberescu
Copy link
Contributor

This will fix #346

Checklist

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 13, 2022

can we make it a little bit prettier? :)

  function viewEta (page, data, opts) {
    if (opts && opts.layout) {
      try {
        layoutIsValid(opts.layout)
        const that = this
        return withLayout(viewEta, opts.layout).call(that, page, data)
      } catch (error) {
        this.send(error)
        return
      }
    }

    if (!page) {
      this.send(new Error('Missing page'))
      return
    }

    lru.define = lru.set
    engine.configure({
      templates: globalOptions.templates ? globalOptions.templates : lru
    })

    const config = Object.assign({
      cache: prod,
      views: templatesDir
    }, globalOptions)

    const sendContent = html => {
      if (
        config.useHtmlMinifier &&
        typeof config.useHtmlMinifier.minify === 'function' &&
        !isPathExcludedMinification(getRequestedPath(this), config.pathsToExcludeHtmlMinifier)
      ) {
        html = config.useHtmlMinifier.minify(
          html,
          config.htmlMinifierOptions || {}
        )
      }
      this.header('Content-Type', 'text/html; charset=' + charset)
      this.send(html)
    }

    data = Object.assign({}, defaultCtx, this.locals, data)
    // Append view extension (Eta will append '.eta' by default,
    // but this also allows custom extensions)
    page = getPage(page, 'eta')
    if (opts?.async ?? globalOptions.async) {
      engine
        .renderFile(page, data, config)
        .then(sendContent)
        .catch(err => this.send(err))
    } else {
      engine.renderFile(page, data, config, (err, html) => {
        err
          ? this.send(err)
          : sendContent(html)
      })
    }
  }

@cberescu
Copy link
Contributor Author

@Uzlopak pushed the code as you proposed.

test/test-eta.js Outdated Show resolved Hide resolved
cberescu and others added 2 commits October 13, 2022 20:10
Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

index.js Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 4b56661 into fastify:master Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors in eta template usinc async mode are not catched
5 participants