Example 2
Read more in https://github.com/sirius-tedarik/sirutils/tree/development/packages/core/src/result
This code snippet demonstrates the use of error handling in JavaScript, utilizing a utility function wrap
from the @sirutils/core
library, and a custom ProjectError
class for managing errors. Here’s a step-by-step explanation:
1. Import Statements:
ProjectError
: A custom error class that likely has methods for creating and throwing errors.wrap
: A utility function that wraps other functions, enabling additional functionality such as automatic error handling or logging.
2. The getUser Function:
- Purpose: This function searches for a user by their
name
in theusers
array. - Logic:
-It attempts to find the user by their name.
- If the user is not found, it creates and throws a
ProjectError
with the message"user not found"
and an error code"?not-found"
. - If the user is found, it returns the user object.
- If the user is not found, it creates and throws a
wrap
: The function is wrapped withwrap
, which likely enhances it by automatically managing the function’s return values, possibly converting thrown errors into structured results.
3. The addUser
Function:
- Purpose: This function attempts to add a new user to the
users
array. - Logic:
- It first checks if a user with the same name already exists using the
getUser
function. - If the user exists (i.e.,
getUser
returned a successful result), it creates and throws aProjectError
with the message "user already exists" and an error code"exists"
. - If the user doesn’t exist, it adds the new user to the
users
array and returnstrue
.
- It first checks if a user with the same name already exists using the
wrap
: LikegetUser
,addUser
is also wrapped, which likely ensures that errors are managed and the function consistently returns a structured result.
4. Executing addUser
and Handling the Result:
- Adding a User: The
addUser
function is called with a user object{ name: "yui", age: 2 }
. - Error Handling:
- If
addUser
returns an error result (result.isErr()
), it logs the error and exits the process. - If no error occurs, it logs the
result.value
, which would betrue
in this case if the user was successfully added.
- If
Summary:
- The code is designed to safely manage adding and retrieving users from a list.
wrap
ensures consistent error handling and result management.ProjectError
provides a structured way to create, throw, and handle errors.- The code flow is designed to stop execution if errors occur, preventing further processing of invalid states.