function mapDockerInterfaceErrorToUiMessage(error) { const code = (error && typeof error === 'object' && error.code) ? String(error.code) : ''; const msg = (error && typeof error === 'object' && typeof error.message === 'string') ? error.message : ''; if (msg && /port is already allocated/i.test(msg)) { return 'That port is already in use. Choose different ports and try again.'; } // Keep messages non-technical. Any technical troubleshooting belongs in a dedicated help surface. switch (code) { case 'PERMISSION_DENIED': return 'Permission denied. Please ensure the required runtime is accessible and try again.'; case 'DAEMON_UNAVAILABLE': return 'The required runtime is not running. Please start it and try again.'; case 'DOCKER_NOT_FOUND': case 'DOCKERODE_MISSING': return 'The required runtime is not available. Please install it and try again.'; case 'INVALID_DOCKER_HOST': return 'The runtime configuration is invalid. Please check your environment settings and try again.'; case 'RUNTIME_NOT_PROVISIONED': return 'The Agent Zero runtime is not set up yet.'; case 'RUNTIME_PROVISION_FAILED': return msg || 'The runtime could not be set up. Please try again.'; case 'RUNTIME_START_FAILED': return msg || 'The runtime could not be started. Please try again.'; case 'RUNTIME_NEEDS_RELOGIN': return 'Docker is installed. Log out and back in once, then return here.'; case 'RUNTIME_MANUAL_INSTALL': return 'A few Docker packages need to be installed first.'; case 'RUNTIME_AUTH_DECLINED': return 'Authentication was cancelled. Nothing was changed.'; case 'RUNTIME_UNSUPPORTED': return 'Automatic Runtime Setup is not available on this system.'; case 'REGISTRY_RATE_LIMIT': return 'Update checks are temporarily unavailable. Please try again later.'; case 'REGISTRY_AUTH_FAILED': return 'Update checks are unavailable. Please try again later.'; case 'REGISTRY_ERROR': case 'REGISTRY_NO_DIGEST': return 'Update checks are unavailable right now. Please try again later.'; case 'DOCKER_PULL_RATE_LIMIT': return 'Docker Hub pull limit reached. Sign in to Docker or try again later.'; case 'GITHUB_API_ERROR': case 'GITHUB_PAGINATION_ERROR': return 'Update checks are unavailable right now. Please try again later.'; case 'OP_IN_PROGRESS': return 'Another operation is already running. Please wait for it to finish.'; case 'OP_NOT_FOUND': return 'No operation is currently running.'; case 'INVALID_OP_ID': case 'INVALID_CONTAINER_ID': return 'Invalid request.'; case 'INVALID_RETENTION_POLICY': return 'Invalid retention setting.'; case 'INVALID_PORT_PREFERENCES': return 'Invalid port settings. Use three different ports (1-65535).'; case 'INVALID_PORT_MAPPINGS': return 'Invalid port mapping. Use host:container, for example 3000:80, or 0:80 to let Docker choose a local port.'; case 'INVALID_ENV_VARS': return 'Invalid environment variables. Use KEY=value lines.'; case 'INVALID_IMAGE': return 'Invalid Docker image name.'; case 'INVALID_MOUNTS': return 'Invalid mount. Use source:/container/path with optional :ro or :rw.'; case 'INVALID_REMOTE_INSTANCE': return 'Enter a valid remote instance URL.'; case 'INVALID_DATA_LOSS_ACK': return 'Please confirm the warning to continue.'; case 'NOT_INSTALLED': return 'This version is not installed yet.'; case 'IMAGE_IN_USE': return 'This version is still used by an Instance. Delete the Instance first, then remove the version.'; case 'NOT_YET_AVAILABLE': return 'This version is not available yet. Please try again later.'; case 'INSTANCE_NOT_FOUND': return 'Instance not found.'; case 'INSTANCE_DELETED_STORAGE_REMAINS': return 'Instance deleted, but its /a0/usr folder could not be removed.'; case 'CANNOT_DELETE_ACTIVE': return 'You cannot delete the active instance.'; case 'NO_RELEASES': return 'No official releases are available right now.'; case 'NO_ACTIVE_INSTANCE': return 'No active instance is available.'; case 'CREATE_FAILED': return 'Unable to create the container.'; case 'CONFLICT': return 'Unable to start due to a conflict (ports or name already in use).'; case 'INVALID_TAG': return 'Invalid Docker image tag.'; case 'TAG_NOT_ALLOWED': return 'That version is not supported.'; case 'INVALID_BACKUP_PATH': return 'Choose a valid Agent Zero backup .zip file.'; case 'BACKUP_NOT_FOUND': return 'Backup file was not found.'; case 'BACKUP_EMPTY': return 'No /a0/usr files were found to back up.'; case 'BACKUP_TOO_LARGE': return msg || 'Backup is too large to export.'; case 'BACKUP_UNAVAILABLE': return 'Backup is not supported by the selected runtime.'; case 'RESTORE_UNAVAILABLE': return 'Restore is not supported by the selected runtime.'; case 'INVALID_BACKUP_ARCHIVE': return msg || 'This backup does not contain restorable /a0/usr data.'; case 'NOT_IMPLEMENTED': return 'This action is not available yet.'; default: return ''; } } function toErrorResponse(error) { const code = (error && typeof error === 'object' && error.code) ? String(error.code) : undefined; const friendly = mapDockerInterfaceErrorToUiMessage(error); const message = friendly || (error && typeof error === 'object' && typeof error.message === 'string' && error.message) || 'Unexpected error'; const payload = { message }; if (code) payload.code = code; return payload; } module.exports = { mapDockerInterfaceErrorToUiMessage, toErrorResponse };