[**CameraKit Web SDK v1.23.1**](../README.md)

***

[CameraKit Web SDK](../globals.md) / findLensScriptError

# Function: findLensScriptError()

> **findLensScriptError**(`error`): [`LensScriptError`](../type-aliases/LensScriptError.md) \| `undefined`

Returns the [LensScriptError](../type-aliases/LensScriptError.md) behind a Lens failure, or `undefined` if none was created. That happens
when the failure wasn't the Lens's script (for example, the Lens could not be downloaded or one of its assets
could not be loaded), or when the script threw something other than an `Error` — see [LensScriptError](../type-aliases/LensScriptError.md).

A Lens script can throw at two moments, and this works the same for both:

```ts
import { findLensScriptError } from '@snap/camera-kit'

// 1. While the Lens is being applied: applyLens rejects with a LensError.
try {
  await cameraKitSession.applyLens(lens)
} catch (error) {
  const scriptError = findLensScriptError(error)
  if (scriptError) console.log(`Lens ${lens.name} failed to start:\n${scriptError.stack}`)
}

// 2. While the Lens is rendering: Camera Kit removes the Lens and emits a LensExecutionError.
cameraKitSession.events.addEventListener('error', ({ detail }) => {
  const scriptError = findLensScriptError(detail.error)
  if (scriptError) {
    console.log(`Lens ${detail.lens.name} has a script error:\n${scriptError.stack}`)
  } else {
    console.log(`Lens ${detail.lens.name} encountered an error and was removed.`)
  }
})
```

The stack looks like this:

```text
LensScriptError: TypeError: cannot read property 'text' of undefined
    at onTap (Scripts/TapHandler.js:18:22)
    at apply (native)
    at <anonymous> (Scripts/TapHandler.js:9:5)
```

## Parameters

### error

`unknown`

Any error emitted by Camera Kit. Values that are not an `Error` return `undefined`.

## Returns

[`LensScriptError`](../type-aliases/LensScriptError.md) \| `undefined`
