@@ -14,6 +14,9 @@ import {
1414 stringPropErrors ,
1515} from "../utils/component" ;
1616import _ from "lodash" ;
17+ import type {
18+ SdkError , Observation ,
19+ } from "@pipedream/types"
1720
1821export type DynamicProps < T extends ConfigurableProps > = { id : string ; configurableProps : T ; } ; // TODO
1922
@@ -24,7 +27,7 @@ export type FormContext<T extends ConfigurableProps> = {
2427 dynamicProps ?: DynamicProps < T > ; // lots of calls require dynamicProps?.id, so need to expose
2528 dynamicPropsQueryIsFetching ?: boolean ;
2629 errors : Record < string , string [ ] > ;
27- sdkErrors : Record < string , string > [ ] ;
30+ sdkErrors : SdkError [ ] ;
2831 fields : Record < string , FormFieldContext < ConfigurableProp > > ;
2932 id : string ;
3033 isValid : boolean ;
@@ -75,7 +78,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({
7578 const id = useId ( ) ;
7679
7780 const {
78- component, configuredProps : __configuredProps , propNames, userId, sdkErrors : __sdkErrors , enableDebugging : __enableDebugging ,
81+ component, configuredProps : __configuredProps , propNames, userId, sdkResponse , enableDebugging : __enableDebugging ,
7982 } = formProps ;
8083 const componentId = component . key ;
8184
@@ -99,7 +102,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({
99102 const [
100103 sdkErrors ,
101104 setSdkErrors ,
102- ] = useState < Record < string , string > [ ] > ( [ ] )
105+ ] = useState < SdkError [ ] > ( [ ] )
103106
104107 const [
105108 enableDebugging
@@ -300,9 +303,9 @@ export const FormContextProvider = <T extends ConfigurableProps>({
300303 ] ) ;
301304
302305 useEffect ( ( ) => {
303- handleSdkErrors ( __sdkErrors )
306+ handleSdkErrors ( sdkResponse )
304307 } , [
305- __sdkErrors ,
308+ sdkResponse ,
306309 ] ) ;
307310
308311 useEffect ( ( ) => {
@@ -436,92 +439,102 @@ export const FormContextProvider = <T extends ConfigurableProps>({
436439 checkPropsNeedConfiguring ( )
437440 } ;
438441
439- const handleSdkErrors = ( o : unknown [ ] | unknown | undefined ) => {
442+ const handleSdkErrors = ( sdkResponse : unknown [ ] | unknown | undefined ) => {
443+ if ( ! sdkResponse ) return
444+
440445 let newErrors = [
441446 ...sdkErrors ,
442447 ]
443- if ( ! o ) return
444- const handleObservationErrors = ( observation : never ) => {
445- const name : string = observation . err ?. name
446- const message : string = observation . err ?. message
447- if ( name && message ) return {
448- name,
449- message,
450- } as Record < string , string >
451- }
452448
453- if ( Array . isArray ( o ) && o . length > 0 ) {
454- for ( let i = 0 ; i < o . length ; i ++ ) {
455- const item = o [ i ]
456- if ( typeof item === "string" ) {
457- try {
458- const json = JSON . parse ( item )
459- const name = json . name
460- const message = json . message
461- if ( name && message ) {
462- newErrors . push ( {
463- name,
464- message,
465- } as Record < string , string > )
466- }
467- // eslint-disable-next-line @typescript-eslint/no-unused-vars
468- } catch ( e ) {
469- // pass
470- }
471- } else if ( typeof item === "object" && "name" in item && "message" in item ) {
472- const name = item . name
473- const message = item . message
474- if ( name && message ) {
475- newErrors . push ( {
476- name,
477- message,
478- } as Record < string , string > )
479- }
480- } else if ( typeof item === "object" && item . k === "error" ) {
481- const res = handleObservationErrors ( item )
482- if ( res ) newErrors . push ( res )
449+ const errorFromString = ( item : string , ret : SdkError [ ] ) => {
450+ try {
451+ const json = JSON . parse ( item )
452+ const err : SdkError = {
453+ name : json . name ,
454+ message : json . message ,
483455 }
456+ if ( err . name && err . message ) {
457+ ret . push ( err )
458+ }
459+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
460+ } catch ( e ) {
461+ // pass
462+ }
463+ }
464+
465+ const errorFromObject = ( item : unknown , ret : SdkError [ ] ) => {
466+ const err : SdkError = {
467+ name : item . name ,
468+ message : item . message ,
469+ }
470+ if ( err . name && err . message ) {
471+ ret . push ( err )
484472 }
485- } else if ( typeof o === "object" && "os" in o || "observations" in o ) {
486- const os = o . os || o . observations
473+ }
474+
475+ const errorFromObservationError = ( item : Error , ret : SdkError [ ] ) => {
476+ const err : SdkError = {
477+ name : item . err ?. name ,
478+ message : item . err ?. message ,
479+ }
480+ if ( err . name && err . message ) {
481+ ret . push ( err )
482+ }
483+ }
484+
485+ const errorFromObservation = ( payload : Observation , ret : SdkError [ ] ) => {
486+ const os = payload . os || payload . observations
487487 if ( Array . isArray ( os ) && os . length > 0 ) {
488- newErrors . push (
489- ...os . filter ( ( it ) => it . k === "error" )
490- . map ( handleObservationErrors )
491- . filter ( ( e ) => e !== undefined ) ,
492- )
488+ for ( let i = 0 ; i < os . length ; i ++ ) {
489+ if ( os [ i ] . k !== "error" ) continue
490+ errorFromObservationError ( os [ i ] , ret )
491+ }
493492 }
494- } else if ( typeof o === "object" && "message" in o ) {
493+ }
494+
495+ const errorFromDetails = ( data : unknown , ret : SdkError [ ] ) => {
496+ ret . push ( {
497+ name : data . error ,
498+ message : JSON . stringify ( data . details ) ,
499+ // message: ` // TODO: It would be nice to render the JSON in markdown
500+ // \`\`\`json
501+ // ${JSON.stringify(data.details)}
502+ // \`\`\`
503+ // `,
504+ // })
505+ } )
506+ }
507+
508+ const errorFromHttpError = ( payload : Error , ret : SdkError [ ] ) => {
495509 // Handle HTTP errors thrown by the SDK
496510 try {
497- const json = JSON . parse ( o . message )
498- const data = json . data
511+ const data = JSON . parse ( payload . message ) ?. data
499512 if ( data && "observations" in data ) {
500- const obs = data . observations || [ ]
501- if ( obs && obs . length > 0 ) {
502- const res = obs . filter ( ( it ) => it . k === "error" )
503- ?. map ( handleObservationErrors )
504- ?. filter ( ( e ) => e !== undefined ) || [ ]
505- newErrors . push (
506- ...res ,
507- )
508- }
513+ errorFromObservation ( data , ret )
509514 } else if ( data && "error" in data && "details" in data ) {
510- newErrors . push ( {
511- name : data . error ,
512- message : JSON . stringify ( data . details ) ,
513- // message: ` // TODO: It would be nice to render the JSON in markdown
514- // \`\`\`json
515- // ${JSON.stringify(data.details)}
516- // \`\`\`
517- // `,
518- // })
519- } )
515+ errorFromDetails ( data , ret )
520516 }
521517 // eslint-disable-next-line @typescript-eslint/no-unused-vars
522518 } catch ( e ) {
523519 // pass
524520 }
521+ }
522+
523+ if ( Array . isArray ( sdkResponse ) && sdkResponse . length > 0 ) {
524+ for ( let i = 0 ; i < sdkResponse . length ; i ++ ) {
525+ const item = sdkResponse [ i ]
526+ if ( typeof item === "string" ) {
527+ errorFromString ( item , newErrors )
528+ } else if ( typeof item === "object" && "name" in item && "message" in item ) {
529+ errorFromObject ( item , newErrors )
530+ } else if ( typeof item === "object" && item . k === "error" ) {
531+ errorFromObservationError ( item , newErrors )
532+ }
533+ }
534+ } else if ( typeof sdkResponse === "object" && "os" in sdkResponse || "observations" in sdkResponse ) {
535+ errorFromObservation ( sdkResponse , newErrors )
536+ } else if ( typeof sdkResponse === "object" && "message" in sdkResponse ) {
537+ errorFromHttpError ( sdkResponse , newErrors )
525538 } else {
526539 newErrors = [ ]
527540 }
0 commit comments