@@ -4,6 +4,35 @@ import { processParametersWithExpressions } from "./expressionProcessor";
44 * Utility functions for routing hooks that process expressions
55 */
66
7+ /**
8+ * Post-receive hook that surfaces API error messages in n8n UI.
9+ * Requires ignoreHttpStatusErrors: true and returnFullResponse: true on the request.
10+ */
11+ export async function handleApiErrorResponse (
12+ this : any ,
13+ items : any [ ] ,
14+ responseData : any ,
15+ ) : Promise < any [ ] > {
16+ const statusCode = responseData ?. statusCode ;
17+ if ( statusCode && statusCode >= 400 ) {
18+ const body = responseData . body ;
19+ let errorMessage = `LATE API Error (${ statusCode } )` ;
20+
21+ if ( typeof body === 'object' && body !== null ) {
22+ if ( body . error ) errorMessage += `: ${ body . error } ` ;
23+ if ( body . details ) {
24+ errorMessage += ` - ${ typeof body . details === 'string' ? body . details : JSON . stringify ( body . details ) } ` ;
25+ }
26+ if ( body . code ) errorMessage += ` [${ body . code } ]` ;
27+ } else if ( typeof body === 'string' ) {
28+ errorMessage += `: ${ body } ` ;
29+ }
30+
31+ throw new Error ( errorMessage ) ;
32+ }
33+ return items ;
34+ }
35+
736/**
837 * Builds the platforms array from selected platforms and accounts
938 */
@@ -24,10 +53,12 @@ function buildPlatformsArray(
2453 itemIndex ,
2554 [ ]
2655 ) as string [ ] ;
27- return accounts . map ( ( id : string ) => ( {
28- platform,
29- accountId : id ,
30- } ) ) ;
56+ return accounts
57+ . filter ( ( id : string ) => id && id !== "none" && id !== "error" && id . length === 24 )
58+ . map ( ( id : string ) => ( {
59+ platform,
60+ accountId : id ,
61+ } ) ) ;
3162 } )
3263 . flat ( ) ;
3364}
@@ -103,14 +134,26 @@ export async function postsCreatePreSend(
103134 0
104135 ) ;
105136
137+ // Build platforms and validate before sending
138+ const platforms = buildPlatformsArray ( this , 0 ) ;
139+ const publishNow = this . getNodeParameter ( "publishNow" , 0 , false ) ;
140+ const isDraft = this . getNodeParameter ( "isDraft" , 0 , false ) ;
141+
142+ if ( ! isDraft && platforms . length === 0 ) {
143+ throw new Error (
144+ 'No valid accounts selected. Please select at least one account for each platform you want to post to. ' +
145+ 'If no accounts appear in the dropdown, make sure you have connected accounts in your LATE dashboard (https://getlate.dev).'
146+ ) ;
147+ }
148+
106149 // Build the body with processed parameters
107150 requestOptions . body = {
108151 content : this . getNodeParameter ( "content" , 0 ) ,
109- platforms : buildPlatformsArray ( this , 0 ) ,
152+ platforms,
110153 scheduledFor : this . getNodeParameter ( "scheduledFor" , 0 , undefined ) ,
111154 timezone : this . getNodeParameter ( "timezone" , 0 , "UTC" ) ,
112- publishNow : this . getNodeParameter ( "publishNow" , 0 , false ) ,
113- isDraft : this . getNodeParameter ( "isDraft" , 0 , false ) ,
155+ publishNow,
156+ isDraft,
114157 visibility : this . getNodeParameter ( "visibility" , 0 , "public" ) ,
115158 tags : buildTagsArray ( this , 0 ) ,
116159 mediaItems : processedParams . mediaItems ?. items || [ ] ,
0 commit comments