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

Add source mapping for errors in routes.match #216

Open
EECOLOR opened this issue Mar 13, 2021 · 0 comments
Open

Add source mapping for errors in routes.match #216

EECOLOR opened this issue Mar 13, 2021 · 0 comments

Comments

@EECOLOR
Copy link
Member

EECOLOR commented Mar 13, 2021

This can be done in the template (hacked version):

Object.assign(
  render,
  withSourceMap(template),
)

function withSourceMap(template) {
  console.log(template)
  return mapValues(template, x =>
    typeof x === 'function' ? (...args) => withSourceMappedErrorAsync(createMap, () => x(...args)) :
    x && typeof x === 'object' ? withSourceMap(x) : x
  )
}
function mapValues(x, f) {
  return Object.entries(x)
    .map(([k, v]) => [k, f(v, k, x)])
    .reduce((result, [k, v]) => ({ ...result, [k]: v }), {})
}
...
function withSourceMappedErrorAsync(createMap, fn, options) {
  return withRawErrorStackAsync(async () => {
    try {
      return await fn()
    } catch (e) {
      const messageWithStack = e + '\n' + toMappedStack(createMap, e.stack, options)
      const error = new Error(messageWithStack)
      error.stack = messageWithStack
      throw error
    }
  })
}
async function withRawErrorStackAsync(fn) {
  const $prepareStackTrace = Error.prepareStackTrace
  Error.prepareStackTrace = (error, stack) => stack
  try { return await fn() } finally { Error.prepareStackTrace = $prepareStackTrace }
}

Another option would be to call the function with source mapped error from serve.

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

No branches or pull requests

1 participant