Error Handling
The client provides specific error types for different scenarios. All errors extend from the base LHUserTasksError
class.
Error Types
UnauthorizedError
Thrown when authentication fails or the access token is invalid.
ForbiddenError
Thrown when the authenticated user doesn't have permission to perform the requested action.
NotFoundError
Thrown when the requested resource doesn't exist.
ValidationError
Thrown when the request contains invalid data.
TaskStateError
Thrown when attempting an operation that's invalid for the current task state (e.g., completing an already completed task).
AssignmentError
Thrown when there's an error related to task assignment (e.g., assigning to an invalid user).
Example Usage
import {
LHUserTasksError,
UnauthorizedError,
ForbiddenError,
NotFoundError,
ValidationError,
TaskStateError,
AssignmentError
} from "@littlehorse-enterprises/user-tasks-api-client";
try {
await client.completeUserTask(task, result);
} catch (error) {
if (error instanceof TaskStateError) {
console.error("Task is in wrong state:", error.message);
} else if (error instanceof UnauthorizedError) {
console.error("Authentication failed:", error.message);
} else if (error instanceof ForbiddenError) {
console.error("Permission denied:", error.message);
} else if (error instanceof NotFoundError) {
console.error("Task not found:", error.message);
} else if (error instanceof ValidationError) {
console.error("Invalid data:", error.message);
} else if (error instanceof AssignmentError) {
console.error("Assignment error:", error.message);
} else if (error instanceof LHUserTasksError) {
console.error("Other LH error:", error.message);
} else {
console.error("Unknown error:", error);
}
}
Error Properties
All User Tasks API Client errors include the following properties:
{
message: string; // Human-readable error message
}