跳转至

User-facing errors(面向用户的错误(User-facing errors))

When running functions in other parts of the platform, such as Workshop or actions, you may want to throw an error with a detailed message. To do so, throw a UserFacingError. For example:

```typescript tab="TypeScript v1" import { Function, UserFacingError } from "@foundry/functions-api"; import { Employee } from "@foundry/ontology-api";

export class MyFunctions { @Function() public async searchExactlyFiveEmployees(employees: Employee[]): Proimse { if (employees.length != 5) { throw new UserFacingError(Pass in exactly 5 employees. Received ${employees.length}.); }

    // search employees
}

} typescript tab="TypeScript v2" import { Osdk } from "@osdk/client"; import { Employee } from "@ontology/sdk"; import { UserFacingError } from "@osdk/functions"; export default async function searchExactlyFiveEmployees(employees: Array>): Promise { if (employees.length != 5) { throw new UserFacingError(Pass in exactly 5 employees. Received ${employees.length}.); } // search employees } ```

```python tab="Python" from functions.api import function, UserFacingError from ontology_sdk import FoundryClient from ontology_sdk.ontology.objects import Aircraft

@function() def search_exactly_five_employees( employees: list[Aircraft] ) -> str: if not len(aircraft) == 5: raise UserFacingError(f"Pass in exactly 5 employees. Received ${len(aircraft)}.")

# search employees

``` When running this as a Function-backed Action in a Workshop application with an incorrect number of employees, users will see the following error: user-facing-error By adding a detailed user facing error message, you can help other users of your Function quickly identify and fix the issue.


中文翻译

面向用户的错误(User-facing errors)

在平台其他部分(如Workshop或操作(Actions))运行函数时,您可能需要抛出带有详细信息的错误。为此,请抛出UserFacingError。例如: typescript tab="TypeScript v1" import { Function, UserFacingError } from "@foundry/functions-api"; import { Employee } from "@foundry/ontology-api"; export class MyFunctions { @Function() public async searchExactlyFiveEmployees(employees: Employee[]): Proimse<string> { if (employees.length != 5) { throw new UserFacingError(`请传入恰好5名员工。实际收到${employees.length}名。`); } // 搜索员工 } }

```typescript tab="TypeScript v2" import { Osdk } from "@osdk/client"; import { Employee } from "@ontology/sdk"; import { UserFacingError } from "@osdk/functions";

export default async function searchExactlyFiveEmployees(employees: Array>): Promise { if (employees.length != 5) { throw new UserFacingError(请传入恰好5名员工。实际收到${employees.length}名。); }

// 搜索员工

} python tab="Python" from functions.api import function, UserFacingError from ontology_sdk import FoundryClient from ontology_sdk.ontology.objects import Aircraft @function() def search_exactly_five_employees( employees: list[Aircraft] ) -> str: if not len(aircraft) == 5: raise UserFacingError(f"请传入恰好5名员工。实际收到${len(aircraft)}名。") # 搜索员工 ```

当在Workshop应用程序中作为函数支持的操作(Function-backed Action)运行,且员工数量不正确时,用户将看到以下错误:

user-facing-error

通过添加详细的面向用户错误消息,您可以帮助函数的其他用户快速识别并解决问题。