# Function Invocations

These events occur when a function is about to be executed or has finished executing.

* [`preFunctionInvoke`](#prefunctioninvoke) - This event is triggered before a function is invoked.
* [`postFunctionInvoke`](#postfunctioninvoke) - This event is triggered after a function has been invoked.
* [`onFunctionException`](#onfunctionexception) - This event is triggered when an exception occurs during the function invocation.

## preFunctionInvoke

This event is triggered before a function is invoked. It allows you to perform actions or modifications before the function execution begins.

### Data Structure

| Data Key    | Type             | Description                             |
| ----------- | ---------------- | --------------------------------------- |
| `arguments` | `ArgumentsScope` | The arguments scope                     |
| `context`   | `IBoxContext`    | The BoxLang Request context             |
| `function`  | `Function`       | The UDF/Function/Closure/Lambda invoked |
| `name`      | `String`         | The name of the function invoked        |

### Example

```groovy
class myListener{

	function preFunctionInvoke( struct data ){
		// Access the function name
		var functionName = data.name;
		
		// Log the function invocation
		log.info("Function invoked: " & functionName);
		
		// You can modify arguments if needed
		data.arguments.set("newArg", "value");
	}
}
```

## postFunctionInvoke

This event is triggered after a function has been invoked. It allows you to perform actions or modifications after the function execution has completed.

### Data Structure

| Data Key    | Type             | Description                             |
| ----------- | ---------------- | --------------------------------------- |
| `arguments` | `ArgumentsScope` | The arguments scope                     |
| `context`   | `IBoxContext`    | The BoxLang Request context             |
| `function`  | `Function`       | The UDF/Function/Closure/Lambda invoked |
| `name`      | `String`         | The name of the function invoked        |
| `result`    | `Any`            | The result of the function call         |

### Example

```groovy
class myListener{
	function postFunctionInvoke( struct data ){
		// Access the function name
		var functionName = data.name;
		
		// Log the function invocation result
		log.info("Function invoked: " & functionName & ", Result: " & data.result);
		
		// You can modify the result if needed
		data.result = "Modified Result";
	}
}
```

## onFunctionException

This event is triggered when an exception occurs during the function invocation. It allows you to handle exceptions gracefully.

### Data Structure

| Data Key    | Type             | Description                                                |
| ----------- | ---------------- | ---------------------------------------------------------- |
| `arguments` | `ArgumentsScope` | The arguments scope                                        |
| `context`   | `IBoxContext`    | The BoxLang Request context                                |
| `function`  | `Function`       | The UDF/Function/Closure/Lambda invoked                    |
| `name`      | `String`         | The name of the function invoked                           |
| `exception` | `Exception`      | The exception that occurred during the function invocation |

### Example

```groovy
class myListener{
	function onFunctionException( struct data ){
		// Access the function name
		var functionName = data.name;
		
		// Log the exception
		log.error("Exception in function: " & functionName & ", Message: " & data.exception.message);
		
		// You can handle the exception or modify the response if needed
		data.exception.message = "Custom error message";
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/function-invocations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
