ExMacOSControl.Error exception (ExMacOSControl v0.1.2)
View SourceStructured error handling for macOS automation operations.
This module provides a comprehensive error handling system with:
- Structured error types for common failure scenarios
- Parsing of osascript error output
- Helpful error messages with remediation steps
- Exception behavior for seamless integration with Elixir error handling
Error Types
The following error types are supported:
:syntax_error- Invalid AppleScript/JXA syntax:execution_error- Runtime error during script execution:timeout- Script execution exceeded the timeout limit:not_found- Script file or application not found:permission_denied- Accessibility or automation permissions required:unsupported_platform- Operation attempted on non-macOS platform
Examples
# Create a syntax error
iex> error = ExMacOSControl.Error.syntax_error("Expected end of line", line: 5)
iex> error.type
:syntax_error
# Parse osascript error output
iex> stderr = "syntax error: Expected end of line but found identifier. (-2741)"
iex> error = ExMacOSControl.Error.parse_osascript_error(stderr, 1)
iex> error.type
:syntax_error
# Get remediation steps
iex> error = ExMacOSControl.Error.permission_denied("Accessibility required")
iex> steps = ExMacOSControl.Error.remediation_steps(error)
iex> Enum.count(steps) > 0
true
# Raise as exception
iex> error = ExMacOSControl.Error.timeout("Script timed out", timeout: 5000)
iex> raise error
** (ExMacOSControl.Error) Script execution timed out after 5000ms
Summary
Types
Error type indicating the category of failure.
Structured error with type, message, and additional details.
Functions
Formats the error as a string for exception messages.
Creates an execution error.
Creates a not found error.
Parses osascript error output into a structured error.
Creates a permission denied error.
Provides remediation steps for an error.
Creates a syntax error.
Creates a timeout error.
Creates an unsupported platform error.
Types
@type error_type() ::
:syntax_error
| :execution_error
| :timeout
| :not_found
| :permission_denied
| :unsupported_platform
Error type indicating the category of failure.
@type t() :: %ExMacOSControl.Error{ __exception__: true, details: map(), message: String.t(), type: error_type() }
Structured error with type, message, and additional details.
Functions
Formats the error as a string for exception messages.
Includes the error message and relevant details, along with remediation steps when applicable.
Creates an execution error.
Parameters
message- Description of the execution erroropts- Optional keyword list with additional details
Examples
iex> error = ExMacOSControl.Error.execution_error("Invalid index")
iex> error.type
:execution_error
Creates a not found error.
Parameters
message- Description of what was not foundopts- Optional keyword list with additional details (e.g.,:app,:file)
Examples
iex> error = ExMacOSControl.Error.not_found("Application not found", app: "Foo")
iex> error.type
:not_found
iex> error.details.app
"Foo"
Parses osascript error output into a structured error.
Analyzes stderr output and exit codes from osascript to determine the error type and extract relevant information.
Parameters
stderr- The stderr output from osascriptexit_code- The exit code from osascript
Examples
iex> stderr = "syntax error: Expected end of line but found identifier. (-2741)"
iex> error = ExMacOSControl.Error.parse_osascript_error(stderr, 1)
iex> error.type
:syntax_error
iex> stderr = "execution error: Finder got an error: Can't get window 1. (-1719)"
iex> error = ExMacOSControl.Error.parse_osascript_error(stderr, 1)
iex> error.type
:execution_error
Creates a permission denied error.
Parameters
message- Description of the permission issueopts- Optional keyword list with additional details
Examples
iex> error = ExMacOSControl.Error.permission_denied("Accessibility permissions required")
iex> error.type
:permission_denied
Provides remediation steps for an error.
Returns a list of actionable steps the user can take to resolve the error.
Parameters
error- The error to provide remediation for
Examples
iex> error = ExMacOSControl.Error.permission_denied("Accessibility required")
iex> steps = ExMacOSControl.Error.remediation_steps(error)
iex> is_list(steps)
true
Creates a syntax error.
Parameters
message- Description of the syntax erroropts- Optional keyword list with additional details (e.g.,:line,:column)
Examples
iex> error = ExMacOSControl.Error.syntax_error("Expected end of line", line: 5)
iex> error.type
:syntax_error
iex> error.details.line
5
Creates a timeout error.
Parameters
message- Description of the timeoutopts- Optional keyword list with additional details (e.g.,:timeoutin milliseconds)
Examples
iex> error = ExMacOSControl.Error.timeout("Script exceeded timeout", timeout: 5000)
iex> error.type
:timeout
iex> error.details.timeout
5000
Creates an unsupported platform error.
Parameters
message- Description of the platform issueopts- Optional keyword list with additional details (e.g.,:platform)
Examples
iex> error = ExMacOSControl.Error.unsupported_platform("Not running on macOS", platform: :linux)
iex> error.type
:unsupported_platform
iex> error.details.platform
:linux