codehike icon indicating copy to clipboard operation
codehike copied to clipboard

Better error when annotation is out of range

Open CristhianMotoche opened this issue 1 year ago • 0 comments

An annotation with ranges out of bounds throws an error that's not very descriptive. Is it possible to catch that error and throw an error with an easier message to understand?

Steps to reproduce

  1. Go to the CodeHike playground: https://stackblitz.com/github/code-hike/v1-starter?file=app%2Fpage.mdx
  2. Modify the first code sample with an annotation that includes a range that it's out of bounds. For example:
```js
const lorem = ipsum(dolor, sit)
// !mark(100)
const [amet, consectetur] = [0, 0]
lorem.adipiscing((sed, elit) => {
  if (sed) {
    amet += elit
  }
})
```

Current behaviour:

Fails with:

Error: Cannot read properties of undefined (reading 'range')
Call Stack
range
node_modules/codehike/dist/code/lines.js (83:30)
applyBlockAnnotation
node_modules/codehike/dist/code/lines.js (55:17)
lines
node_modules/codehike/dist/code/pre.js (41:32)
ej
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:264151)
eval
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:276953)
Object.toJSON
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:281723)
stringify
<anonymous>
eval
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:268079)
ez
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:268158)
eH
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:268559)
Timeout.eval [as _onTimeout]
file:///home/projects/github-9xqabe/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (35:265057)
<unknown>
https://github9xqabe-kqha.w-credentialless-staticblitz.com/builtins.ddb8d84d.js (246:4692)

Expected behaviour:

I think it makes sense to throw an error but it would better if the error is more readable. Maybe something like:

Error: Cannot generate a valid range for the given code

Let me know if this makes sense. Thanks!

CristhianMotoche avatar Nov 29 '24 16:11 CristhianMotoche