# Debugging Transformation Errors

This guide lists the different errors you might encounter while using transformations and libraries, and how to fix them.

## JavaScript

#### **ReferenceError**

This error occurs when a non-existent variable is referenced somewhere in the transformation.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>export function transformEvent(event, metadata) {<br/>   event.someValue = value; <br/>   return event;<br/>}</pre> <br />**Error**:<pre>"ReferenceError: value is not defined \n"<br />at transformEvent (base transformation: 2:25)</pre>| Make sure variable is declared and available in the scope. You can also use an optional check:<br /> <pre>if(value) {<br />  event.someValue = value;<br />}</pre> |
| Library code:<pre>// jslibsub<br />export function sub(a,b) {<br />  return a-b-c;<br />}</pre>Transformation code:<pre>import { sub } from 'jslibsub';<br /><br />export function transformEvent(event, metadata) {<br />  event.sub = sub(1, 2)<br />  return event;<br />}</pre><br />**Error**:<pre>ReferenceError: c is not defined<br />  at sub (library jslibsub:2:14)<br />  at transformEvent (base transformation:4:17)</pre>|  Make sure variable is declared and available in the scope. <br /><br />See [Error message format](#transformation-referencing-a-library) for more context on the error message. |

#### **TypeError**

This error occurs when a property is read or function is called on an undefined variable.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>export function transformEvent(event, metadata) {<br/>   event['val']=event.context.traits.obj.val1; <br/>   return event;<br/>}</pre> <br />**Error**:<pre>"TypeError: Cannot read properties of"<br />undefined (reading 'val1')\n <br />at transformEvent (base transformation:2:43)</pre>   |  Use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) when accessing nested fields. <br /> <pre>event.val = event.context?.traits?.obj?.val1;</pre> |

#### **SyntaxError**

This error occurs when you run a syntactically invalid code, that is, it does not conform to the language syntax. For example, `SyntaxError: Unexpected identifier`, `SyntaxError: Unexpected token`.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>export function transformEvent(event, metadata) {<br/> returns event; <br/>}</pre> <br />**Error**:<pre>SyntaxError: Unexpected identifier<br />[base transformation:2:13]</pre>   |  Check your code for any misspelt keywords and missing operators/commas. |

#### **Expected...Found error**

This error occurs when you specify a transformation function other than `transformEvent` or `transformBatch`.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>export function transformEvent2(event, metadata) {<br/>   return event; <br/>}</pre><br />**Error**: <pre>Error: Expected one of transformEvent,transformBatch. <br />Found</pre> | Verify that the transformation function is either `transformEvent` or `transformBatch`. | 

#### **Import error**

This error occurs when the module you are tring to import from a library does not exist.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>import {add} from 'jsLib'; <br/>export function transformEvent(event, metadata) {<br/>   return event; <br/>}</pre> <br />**Error**:<pre>Error: import from jsLib failed. Module not found.</pre>  | Verify that the module you are trying to import exists in the transformation library. | 

## Python

#### **NameError**

This error occurs when when the transformation tries to access or use a variable that has not been defined or assigned a value.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>def transformEvent(event, metadata):<br/>   event['some_value']=value<br/>   return event</pre><br />**Error**:<pre>NameError("name 'value' is not defined")<br />  at transformEvent (base transformation: line 2)<br />  event['some_value']=value</pre>   |  Make sure variable is declared and available in the scope. You can also use an optional check:<br /> <pre>if(value):<br />  event.someValue = value;<br /></pre> |
| Library code:<pre>def sub(a, b):<br />  return a-b-c</pre>Transformation code:<pre>from pylibsub import sub<br /><br />def transformEvent(event, metadata):<br />  event['sub'] = sub(1,2)<br />  return event</pre><br />**Error**:<pre>NameError(\"name 'c' is not defined\")<br />  at sub (library pylibsub: line 2)<br />   return a-b-c<br />  at transformEvent (base transformation: line 4)<br />  event['sub'] = sub(1,2)</pre>| Make sure variable is declared and available in the scope.<br /><br />See [Error message format](#transformation-referencing-a-library) for more context on the error message. | 

#### **ZeroDivisionError**

This error occurs when when the transformation attempts to divide a number by zero.

| Sample code and error | Possible solution |
| :----| :-----|
|  <pre>def transformEvent(event, metadata):<br/>   event['value']=0/0<br/>   return event</pre><br />**Error**:<pre>ZeroDivisionError('division by zero')<br />   at transformEvent (base transformation: line 2)<br />   event['val']=0/0</pre>   |  Verify your transformation logic. |

#### **KeyError**

This error occurs when the transformation attempts to access a dictionary item that does not exist.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>def transformEvent(event, metadata):<br/>   event['val']=event['context']['traits']['obj']['val1']<br/>   return event</pre> <br />**Error**:<pre>KeyError('obj')<br />   at transformEvent (base transformation: line 2)<br />   event['val']=event ['context']['traits']['obj']['val1']</pre>    |  Verify the key that transformation is attempting to access exists in the dictionary. You can also set a optional chaining event like:<br /> <pre>event['val'] = event.get('context').get(<br />'traits',{}).get('obj', {}).get('val1')</pre> |

#### **BadCodeError**

This error occurs when the transformation contains syntactically invalid code or is trying to import an unsupported module/package.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>def transformEvent(event, metadata):<br/>   returns event</pre><br />**Error**:<pre>BadCodeError('invalid syntax (<unknown>, line 2)')</pre>    |  Verify the syntax of your transformation code. |
| <pre>from pyLib2 import add<br />def transformEvent(event, metadata):<br/>   return event</pre> <br />**Error**: <pre>BadCodeError("Unpermitted import(s). Supported modules /<br />packages for import: {'sampleTransformationLibrary',<br />'time', 'random', 'base64', 'string', 'dateutil',<br />'hashlib', 'uuid', 'urllib', 're', 'rudderEmail',<br />'json', 'collections', 'datetime', 'ast', 'requests',<br />'math', 'user_transformation', 'hmac'}")</pre>  |  Verify that the library imports one of the supported built-in packages and user-written libraries only. |


#### **Exception error**

This error occurs when you specify a transformation function other than `transformEvent` or `transformBatch`.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>def transformEvent2(event, metadata):<br/>   return event</pre> <br />**Error**:<pre>Exception("Expected one of ['transformEvent', 'transformBatch'].<br />Found []")</pre>    |  Verify that the transformation function is either `transformEvent` or `transformBatch`. |

#### **ImportError**

This error occurs when the module you are tring to import from a library does not exist or is not found.

| Sample code and error | Possible solution |
| :----| :-----|
| <pre>from pyLib1 import adds<br />def transformEvent(event, metadata):<br/>   return event</pre> <br />**Error**:<pre>ImportError("cannot import name 'adds' <br />from 'pyLib1' <redacted_path_to_pyLib1>")</pre>  | Verify that the module you are trying to import exists in the transformation library.  |

## Error message format

#### **Standalone transformation**

A typical JavaScript transformation error is shown:

```text
ReferenceError: c is not defined
    at sub (library jssublib:2:14)
    at transformEvent (base transformation:4:22)
```

You can interpret the above error as follows:

- `ReferenceError: c is not defined`: The actual error message.
- `at sub (library jssublib:2:14)`: Module in which the error occurs (`line: position`).
- `at transformEvent (base transformation:4:22)`: Depicts where the module is referenced in the transformation (`line: position`).

#### **Transformation referencing a library**

A typical Python transformation error is shown:

```text
NameError(\"name 'c' is not defined\")
    at sub (library pylibsub: line 2)
    return a-b-c
    at transformEvent (base transformation: line 4)
    event['sub'] = sub(1,2)
```

You can interpret the above error as follows:

- `NameError("name 'c' is not defined")`: The actual error message.
- `at sub (library pylibsub: line 2)`: Line at which error occurred in the library.
- `return a-b-c`: Line where the error occurred.
- `at transformEvent (base transformation: line 4)`: Line at which library is referenced by the base transformation.
- `event['sub'] = sub(1,2)`: Line where `lib` function is called.

<br />
