@notionhq/custom-blocks-host 0.2.9 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,171 +1,125 @@
1
- import type {
2
- CustomBlockInitErrorCode,
3
- CustomBlockInitErrorInfo,
4
- } from "@notionhq/custom-blocks-protocol/messages/init.js"
5
-
6
- const UNKNOWN_ERROR_MESSAGE =
7
- "Unexpected error while initializing custom block."
1
+ import type { CustomBlockErrorInfo } from "@notionhq/custom-blocks-protocol/errors.js"
8
2
 
9
3
  /**
10
- * Returns the error info for a given `init` failure code.
11
- *
12
- * `init` handshakes are one-shot: after an `init` error, this host instance will not answer another
13
- * `connect` message. `isRetryable` means a fresh iframe / `init` attempt could plausibly succeed
14
- * without changing block code or host configuration.
4
+ * Initialization handshakes are one-shot: after an error, this host instance will not answer
5
+ * another `connect` message. `isRetryable` means a fresh host and iframe could plausibly
6
+ * initialize without changing block code or host configuration.
15
7
  */
16
- export function initErrorForFailureReason(
17
- reason: CustomBlockInitErrorCode,
18
- data: {
19
- minBridgeProtocolVersion?: number
20
- currentBridgeProtocolVersion?: number
21
- } = {},
22
- ): CustomBlockInitErrorInfo {
23
- switch (reason) {
24
- case "no_connect":
25
- return {
26
- code: reason,
27
- message: "Sandbox did not send a connect message.",
28
- isRetryable: true,
29
- }
30
- case "invalid_connect_payload":
31
- return {
32
- code: reason,
33
- message: "Sandbox sent an invalid connect message.",
34
- // The sandbox must be updated to send a valid connect message.
35
- isRetryable: false,
36
- }
37
- case "manifest_unavailable":
38
- return {
39
- code: reason,
40
- message: "The custom block manifest is unavailable.",
41
- isRetryable: true,
42
- }
43
- case "manifest_invalid":
44
- return {
45
- code: reason,
46
- message: "Sandbox sent an invalid manifest.",
47
- // The sandbox must be updated to a valid manifest.
48
- isRetryable: false,
49
- }
50
- case "invalid_protocol_version": {
51
- const versionText = data.currentBridgeProtocolVersion
52
- ? `: ${data.currentBridgeProtocolVersion}`
53
- : "."
54
- return {
55
- code: reason,
56
- message: `Sandbox reported an invalid bridge protocol version ${versionText}`,
57
- // The sandbox must be updated to a supported protocol version.
58
- isRetryable: false,
59
- }
60
- }
61
- case "unsupported_protocol_version": {
62
- const currentVersionText = data.currentBridgeProtocolVersion
63
- ? `${data.currentBridgeProtocolVersion} `
64
- : ""
65
- const minVersionText = data.minBridgeProtocolVersion
66
- ? ` Minimum supported version is ${data.minBridgeProtocolVersion}.`
67
- : ""
68
- return {
69
- code: reason,
70
- message: `Sandbox bridge protocol version ${currentVersionText}is not supported.${minVersionText}`,
71
- // The sandbox must be updated to a supported protocol version.
72
- isRetryable: false,
73
- }
74
- }
75
- case "init_timeout":
76
- return {
77
- code: reason,
78
- message: "Host dependencies did not finish loading before the timeout.",
79
- isRetryable: true,
80
- }
81
- case "context_unavailable":
82
- return {
83
- code: reason,
84
- message: "Host page context is unavailable.",
85
- isRetryable: true,
86
- }
87
- case "current_user_unavailable":
88
- return {
89
- code: reason,
90
- message: "Current user is unavailable.",
91
- isRetryable: true,
92
- }
93
- case "initial_state_timeout":
94
- return {
95
- code: reason,
96
- message: "Host did not prepare initial state before the timeout.",
97
- isRetryable: true,
98
- }
99
- case "missing_data_source_binding":
100
- return {
101
- code: reason,
102
- message: "A required data source is not mapped.",
103
- // Host configuration must be changed before this block can initialize.
104
- isRetryable: false,
105
- }
106
- case "data_source_unavailable":
107
- return {
108
- code: reason,
109
- message: "A mapped data source is unavailable.",
110
- // This error combines some retryable errors (e.g. data source failed to load) with
111
- // some non-retryable errors (e.g. data source does not exist or user does not have
112
- // access). Fallback to assuming the error is retryable.
113
- isRetryable: true,
114
- }
115
- case "missing_property_binding":
116
- return {
117
- code: reason,
118
- message: "A required data source property is not mapped.",
119
- // Host configuration must be changed before this block can initialize.
120
- isRetryable: false,
121
- }
122
- case "invalid_property_binding":
123
- return {
124
- code: reason,
125
- message: "A mapped data source property is missing or incompatible.",
126
- // The bound property's schema must be updated or the binding must be updated to
127
- // a property with a compatible schema.
128
- isRetryable: false,
129
- }
130
- case "no_init_result":
131
- return {
132
- code: reason,
133
- message: "Sandbox did not acknowledge initialization.",
134
- isRetryable: true,
135
- }
136
- case "invalid_init_result_payload":
137
- return {
138
- code: reason,
139
- message: "Sandbox sent an invalid initialization result.",
140
- isRetryable: false,
141
- }
142
- case "invalid_init_bindings":
143
- return {
144
- code: reason,
145
- message: "Sandbox rejected the host-provided data source bindings.",
146
- isRetryable: false,
147
- }
148
- case "not_in_iframe":
149
- return {
150
- code: reason,
151
- message: "<NotionCustomBlock> only works inside an iframe.",
152
- // The block must be embedded in an iframe.
153
- isRetryable: false,
154
- }
155
- case "unknown_error":
156
- return {
157
- code: reason,
158
- message: UNKNOWN_ERROR_MESSAGE,
159
- // Treat unknown errors as non-retryable.
160
- isRetryable: false,
161
- }
162
- default:
163
- console.warn(`Unexpected custom block init error: ${reason}`)
164
- return {
165
- code: reason,
166
- message: UNKNOWN_ERROR_MESSAGE,
167
- // Treat unknown error codes as non-retryable.
168
- isRetryable: false,
169
- }
8
+ export function noConnectError(): CustomBlockErrorInfo<"no_connect"> {
9
+ return {
10
+ code: "no_connect",
11
+ message: "Sandbox did not send a connect message.",
12
+ // A fresh iframe may finish loading and send connect before the timeout.
13
+ isRetryable: true,
14
+ }
15
+ }
16
+
17
+ export function invalidConnectPayloadError(): CustomBlockErrorInfo<"invalid_connect_payload"> {
18
+ return {
19
+ code: "invalid_connect_payload",
20
+ message: "Sandbox sent an invalid connect message.",
21
+ // The sandbox must be updated to send a valid connect message.
22
+ isRetryable: false,
23
+ }
24
+ }
25
+
26
+ export function invalidProtocolVersionError(args: {
27
+ currentBridgeProtocolVersion: number | undefined
28
+ }): CustomBlockErrorInfo<"invalid_protocol_version"> {
29
+ const { currentBridgeProtocolVersion } = args
30
+ const versionText = currentBridgeProtocolVersion
31
+ ? `: ${currentBridgeProtocolVersion}`
32
+ : "."
33
+ return {
34
+ code: "invalid_protocol_version",
35
+ message: `Sandbox reported an invalid bridge protocol version ${versionText}`,
36
+ // The sandbox must be updated to a supported protocol version.
37
+ isRetryable: false,
38
+ }
39
+ }
40
+
41
+ export function unsupportedProtocolVersionError(args: {
42
+ currentBridgeProtocolVersion: number
43
+ minBridgeProtocolVersion: number
44
+ }): CustomBlockErrorInfo<"unsupported_protocol_version"> {
45
+ const { currentBridgeProtocolVersion, minBridgeProtocolVersion } = args
46
+ return {
47
+ code: "unsupported_protocol_version",
48
+ message: `Sandbox bridge protocol version ${currentBridgeProtocolVersion} is not supported. Minimum supported version is ${minBridgeProtocolVersion}.`,
49
+ // The sandbox must be updated to a supported protocol version.
50
+ isRetryable: false,
51
+ }
52
+ }
53
+
54
+ export function contextUnavailableError(): CustomBlockErrorInfo<"context_unavailable"> {
55
+ return {
56
+ code: "context_unavailable",
57
+ message: "Host page context is unavailable.",
58
+ // Page context may become available before a fresh initialization attempt.
59
+ isRetryable: true,
60
+ }
61
+ }
62
+
63
+ export function initialStateTimeoutError(): CustomBlockErrorInfo<"initial_state_timeout"> {
64
+ return {
65
+ code: "initial_state_timeout",
66
+ message: "Host did not prepare initial state before the timeout.",
67
+ // A fresh attempt may finish loading host dependencies before the timeout.
68
+ isRetryable: true,
69
+ }
70
+ }
71
+
72
+ export function missingDataSourceBindingError(): CustomBlockErrorInfo<"missing_data_source_binding"> {
73
+ return {
74
+ code: "missing_data_source_binding",
75
+ message: "A required data source is not mapped.",
76
+ // Host configuration must be changed before this block can initialize.
77
+ isRetryable: false,
78
+ }
79
+ }
80
+
81
+ export function missingPropertyBindingError(): CustomBlockErrorInfo<"missing_property_binding"> {
82
+ return {
83
+ code: "missing_property_binding",
84
+ message: "A required data source property is not mapped.",
85
+ // Host configuration must be changed before this block can initialize.
86
+ isRetryable: false,
87
+ }
88
+ }
89
+
90
+ export function invalidPropertyBindingError(): CustomBlockErrorInfo<"invalid_property_binding"> {
91
+ return {
92
+ code: "invalid_property_binding",
93
+ message: "A mapped data source property is missing or incompatible.",
94
+ // The bound property's schema must be updated or the binding must be updated to
95
+ // a property with a compatible schema.
96
+ isRetryable: false,
97
+ }
98
+ }
99
+
100
+ export function noInitResultError(): CustomBlockErrorInfo<"no_init_result"> {
101
+ return {
102
+ code: "no_init_result",
103
+ message: "Sandbox did not acknowledge initialization.",
104
+ // A fresh iframe may process init and acknowledge it before the timeout.
105
+ isRetryable: true,
106
+ }
107
+ }
108
+
109
+ export function invalidInitResultPayloadError(): CustomBlockErrorInfo<"invalid_init_result_payload"> {
110
+ return {
111
+ code: "invalid_init_result_payload",
112
+ message: "Sandbox sent an invalid initialization result.",
113
+ // The sandbox must be updated to send a valid initialization result.
114
+ isRetryable: false,
115
+ }
116
+ }
117
+
118
+ export function unknownInitializationError(): CustomBlockErrorInfo<"unknown_error"> {
119
+ return {
120
+ code: "unknown_error",
121
+ message: "Unexpected error while initializing custom block.",
122
+ // Without a known transient cause, conservatively treat unknown errors as non-retryable.
123
+ isRetryable: false,
170
124
  }
171
125
  }
@@ -8,7 +8,14 @@ import type { InitResultMessage } from "@notionhq/custom-blocks-protocol/message
8
8
  import type { InferInput } from "valibot"
9
9
  import type { CustomBlockHostFailure } from "../messages/types.js"
10
10
  import { unreachable } from "../utils.js"
11
- import { initErrorForFailureReason } from "./initErrors.js"
11
+ import {
12
+ initialStateTimeoutError,
13
+ invalidProtocolVersionError,
14
+ noConnectError,
15
+ noInitResultError,
16
+ unknownInitializationError,
17
+ unsupportedProtocolVersionError,
18
+ } from "./initErrors.js"
12
19
  import { isValidBridgeProtocolVersion } from "./protocolVersion.js"
13
20
  import type {
14
21
  ConnectSuccessMessage,
@@ -141,7 +148,7 @@ export function createInitializationController(
141
148
  if (initStatus !== "preparingInit") {
142
149
  return
143
150
  }
144
- const error = initErrorForFailureReason("initial_state_timeout")
151
+ const error = initialStateTimeoutError()
145
152
  warn({
146
153
  ...error,
147
154
  initializationId: connect.initializationId,
@@ -183,7 +190,7 @@ export function createInitializationController(
183
190
  return
184
191
  }
185
192
  initStatus = "error"
186
- const error = initErrorForFailureReason("no_init_result")
193
+ const error = noInitResultError()
187
194
  onConnectionFailure?.(error)
188
195
  warn({
189
196
  ...error,
@@ -200,7 +207,7 @@ export function createInitializationController(
200
207
  })
201
208
  sendInitError({
202
209
  initializationId: connect.initializationId,
203
- error: initErrorForFailureReason("unknown_error"),
210
+ error: unknownInitializationError(),
204
211
  })
205
212
  }
206
213
  }
@@ -215,7 +222,7 @@ export function createInitializationController(
215
222
 
216
223
  // Verify the bridge protocol version is valid.
217
224
  if (!isValidBridgeProtocolVersion(message.bridgeProtocolVersion)) {
218
- const initError = initErrorForFailureReason("invalid_protocol_version", {
225
+ const initError = invalidProtocolVersionError({
219
226
  currentBridgeProtocolVersion: message.bridgeProtocolVersion,
220
227
  })
221
228
  sendInitError({
@@ -225,13 +232,10 @@ export function createInitializationController(
225
232
  return
226
233
  }
227
234
  if (message.bridgeProtocolVersion < minBridgeProtocolVersion) {
228
- const initError = initErrorForFailureReason(
229
- "unsupported_protocol_version",
230
- {
231
- currentBridgeProtocolVersion: message.bridgeProtocolVersion,
232
- minBridgeProtocolVersion,
233
- },
234
- )
235
+ const initError = unsupportedProtocolVersionError({
236
+ currentBridgeProtocolVersion: message.bridgeProtocolVersion,
237
+ minBridgeProtocolVersion,
238
+ })
235
239
  sendInitError({
236
240
  initializationId: message.initializationId,
237
241
  error: initError,
@@ -273,9 +277,11 @@ export function createInitializationController(
273
277
  // when the malformed message includes a numeric version.
274
278
  if (bridgeProtocolVersion !== undefined) {
275
279
  if (!isValidBridgeProtocolVersion(bridgeProtocolVersion)) {
276
- error = initErrorForFailureReason("invalid_protocol_version")
280
+ error = invalidProtocolVersionError({
281
+ currentBridgeProtocolVersion: undefined,
282
+ })
277
283
  } else if (bridgeProtocolVersion < minBridgeProtocolVersion) {
278
- error = initErrorForFailureReason("unsupported_protocol_version", {
284
+ error = unsupportedProtocolVersionError({
279
285
  currentBridgeProtocolVersion: bridgeProtocolVersion,
280
286
  minBridgeProtocolVersion,
281
287
  })
@@ -329,7 +335,7 @@ export function createInitializationController(
329
335
  return
330
336
  }
331
337
  initStatus = "error"
332
- const error = initErrorForFailureReason("no_connect")
338
+ const error = noConnectError()
333
339
  warn(error)
334
340
  onConnectionFailure?.(error)
335
341
  }, noConnectTimeoutMs)
@@ -13,7 +13,10 @@ import { sandboxToHostMessageSchema } from "@notionhq/custom-blocks-protocol/mes
13
13
  import type { UnsubscribeDataSourceQueryMessage } from "@notionhq/custom-blocks-protocol/messages/unsubscribeDataSourceQuery.js"
14
14
  import type { UpdatePageMessage } from "@notionhq/custom-blocks-protocol/messages/updatePage.js"
15
15
  import * as v from "valibot"
16
- import { initErrorForFailureReason } from "../lifecycle/initErrors.js"
16
+ import {
17
+ invalidConnectPayloadError,
18
+ invalidInitResultPayloadError,
19
+ } from "../lifecycle/initErrors.js"
17
20
  import type { InitializationController } from "../lifecycle/initializationController.js"
18
21
  import type { InitStatus } from "../lifecycle/types.js"
19
22
  import { unreachable } from "../utils.js"
@@ -95,7 +98,7 @@ export function createSandboxMessageRouter(
95
98
  initializationId: parsedIdentity.success
96
99
  ? parsedIdentity.output.initializationId
97
100
  : undefined,
98
- error: initErrorForFailureReason("invalid_connect_payload"),
101
+ error: invalidConnectPayloadError(),
99
102
  bridgeProtocolVersion: parsedVersion.success
100
103
  ? parsedVersion.output.bridgeProtocolVersion
101
104
  : undefined,
@@ -112,7 +115,7 @@ export function createSandboxMessageRouter(
112
115
  initializationId: parsedIdentity.success
113
116
  ? parsedIdentity.output.initializationId
114
117
  : undefined,
115
- error: initErrorForFailureReason("invalid_init_result_payload"),
118
+ error: invalidInitResultPayloadError(),
116
119
  })
117
120
  }
118
121