import type { CustomBlockInitErrorCode, CustomBlockInitErrorInfo, } from "../../bridge/messages/init.js" const UNKNOWN_ERROR_MESSAGE = "Unexpected error while initializing custom block." /** * Returns the error info for a given `init` failure code. * * `init` handshakes are one-shot: after an `init` error, this host instance will not answer another * `ready` message. `isRetryable` means a fresh iframe / `init` attempt could plausibly succeed * without changing block code or host configuration. */ export function initErrorForFailureReason( reason: CustomBlockInitErrorCode, ): CustomBlockInitErrorInfo { switch (reason) { case "no_ready": return { code: reason, message: "Sandbox did not send a ready message.", isRetryable: true, } case "invalid_ready": return { code: reason, message: "Sandbox sent an invalid ready message.", // The sandbox must be updated to send a valid ready message. isRetryable: false, } case "manifest_unavailable": return { code: reason, message: "Sandbox could not load its manifest.", isRetryable: true, } case "manifest_invalid": return { code: reason, message: "Sandbox sent an invalid manifest.", // The sandbox must be updated to a valid manifest. isRetryable: false, } case "invalid_protocol_version": return { code: reason, message: "Sandbox reported an invalid bridge protocol version.", // The sandbox must be updated to a supported protocol version. isRetryable: false, } case "unsupported_protocol_version": return { code: reason, message: "Sandbox bridge protocol version is not supported.", // The sandbox must be updated to a supported protocol version. isRetryable: false, } case "context_unavailable": return { code: reason, message: "Host page context is unavailable.", isRetryable: true, } case "current_user_unavailable": return { code: reason, message: "Current user is unavailable.", isRetryable: true, } case "missing_data_source_binding": return { code: reason, message: "A required data source is not mapped.", // Host configuration must be changed before this block can initialize. isRetryable: false, } case "data_source_unavailable": return { code: reason, message: "A mapped data source is unavailable.", // This error combines some retryable errors (e.g. data source failed to load) with // some non-retryable errors (e.g. data source does not exist or user does not have // access). Fallback to assuming the error is retryable. isRetryable: true, } case "missing_property_binding": return { code: reason, message: "A required data source property is not mapped.", // Host configuration must be changed before this block can initialize. isRetryable: false, } case "invalid_property_binding": return { code: reason, message: "A mapped data source property is missing or incompatible.", // The bound property's schema must be updated or the binding must be updated to // a property with a compatible schema. isRetryable: false, } case "not_in_iframe": return { code: reason, message: " only works inside an iframe.", // The block must be embedded in an iframe. isRetryable: false, } case "init_timeout": return { code: reason, message: "Host did not respond to init before the timeout.", isRetryable: true, } case "unknown_error": return { code: reason, message: UNKNOWN_ERROR_MESSAGE, // Treat unknown errors as non-retryable. isRetryable: false, } default: console.warn(`Unexpected custom block init error: ${reason}`) return { code: reason, message: UNKNOWN_ERROR_MESSAGE, // Treat unknown error codes as non-retryable. isRetryable: false, } } }