@@ -14,6 +14,27 @@ export const BROWSER_ERROR_REASONS = {
1414 noDisplayForHeadedProfile : "no_display_for_headed_profile" ,
1515} as const ;
1616
17+ /** Stable machine-readable codes returned by browser action routes. */
18+ export const BROWSER_ACT_ERROR_CODES = {
19+ kindRequired : "ACT_KIND_REQUIRED" ,
20+ invalidRequest : "ACT_INVALID_REQUEST" ,
21+ selectorUnsupported : "ACT_SELECTOR_UNSUPPORTED" ,
22+ evaluateDisabled : "ACT_EVALUATE_DISABLED" ,
23+ unsupportedForExistingSession : "ACT_EXISTING_SESSION_UNSUPPORTED" ,
24+ targetIdMismatch : "ACT_TARGET_ID_MISMATCH" ,
25+ } as const ;
26+
27+ export type BrowserActErrorCode =
28+ ( typeof BROWSER_ACT_ERROR_CODES ) [ keyof typeof BROWSER_ACT_ERROR_CODES ] ;
29+
30+ const BROWSER_ACT_ERROR_CODE_VALUES : ReadonlySet < string > = new Set (
31+ Object . values ( BROWSER_ACT_ERROR_CODES ) ,
32+ ) ;
33+
34+ function isBrowserActErrorCode ( value : unknown ) : value is BrowserActErrorCode {
35+ return typeof value === "string" && BROWSER_ACT_ERROR_CODE_VALUES . has ( value ) ;
36+ }
37+
1738const NO_DISPLAY_HEADLESS_SOURCES = [ "request" , "env" , "profile" , "config" , "default" ] as const ;
1839
1940export type BrowserNoDisplayErrorDetails = {
@@ -30,7 +51,11 @@ export type BrowserNoDisplayErrorMetadata = {
3051
3152type WithNoDisplayMetadata < T > = T | ( T & BrowserNoDisplayErrorMetadata ) ;
3253export type BrowserErrorResponse = WithNoDisplayMetadata < { status : number ; message : string } > ;
33- type BrowserErrorPayload = WithNoDisplayMetadata < { error : string } > ;
54+ export type BrowserErrorPayload = WithNoDisplayMetadata < {
55+ error : string ;
56+ code ?: BrowserActErrorCode ;
57+ unrecognizedCode ?: true ;
58+ } > ;
3459
3560/** Base browser error carrying an HTTP status code. */
3661export class BrowserError extends Error {
@@ -186,11 +211,19 @@ export function parseBrowserErrorPayload(value: unknown): BrowserErrorPayload |
186211 if ( typeof body . error !== "string" || body . error . length === 0 ) {
187212 return null ;
188213 }
214+ const code = isBrowserActErrorCode ( body . code ) ? body . code : undefined ;
215+ const unrecognizedCode =
216+ body . unrecognizedCode === true || ( body . code !== undefined && ! code ) ? true : undefined ;
217+ const actionCode : { code ?: BrowserActErrorCode ; unrecognizedCode ?: true } = code
218+ ? { code }
219+ : unrecognizedCode
220+ ? { unrecognizedCode : true }
221+ : { } ;
189222 if ( body . reason === BROWSER_ERROR_REASONS . noDisplayForHeadedProfile ) {
190223 const details = parseNoDisplayDetails ( body . details ) ;
191224 if ( details ) {
192- return { error : body . error , reason : body . reason , details } ;
225+ return { error : body . error , ... actionCode , reason : body . reason , details } ;
193226 }
194227 }
195- return { error : body . error } ;
228+ return { error : body . error , ... actionCode } ;
196229}
0 commit comments