/**
 * Error handling utilities for the Host application.
 *
 * Provides a consistent way to create error responses matching
 * the MessageError format defined in @pocketcoder/shared.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
export { ErrorCode };
/**
 * Create a formatted error object matching the MessageError interface.
 *
 * @param code - ErrorCode enum value
 * @param message - Human-readable error description
 * @param details - Optional additional context
 */
export function createError(code, message, details) {
    const error = { code, message };
    if (details !== undefined) {
        error.details = details;
    }
    return error;
}
/**
 * Application-level error class that carries an ErrorCode.
 * Handlers can throw this and the router/catch block can extract
 * structured error information.
 */
export class AppError extends Error {
    code;
    details;
    constructor(code, message, details) {
        super(message);
        this.name = "AppError";
        this.code = code;
        this.details = details;
    }
    /** Convert to a MessageError for sending over the wire. */
    toMessageError() {
        return createError(this.code, this.message, this.details);
    }
}
/**
 * Extract a MessageError from an unknown caught value.
 * If the value is an AppError, uses its code and details.
 * Otherwise falls back to UNKNOWN_ERROR.
 */
export function toMessageError(err) {
    if (err instanceof AppError) {
        return err.toMessageError();
    }
    const message = err instanceof Error ? err.message : String(err);
    return createError(ErrorCode.UNKNOWN_ERROR, message);
}
//# sourceMappingURL=index.js.map