@@ -137,11 +137,35 @@ async function resolvePageId(aliasOrId, filterInput) {
137137// ─── Lazy Notion client ────────────────────────────────────────────────────────
138138
139139let _notion = null ;
140+ let _notionWithRetry = null ;
141+
142+ function wrapNotionClient ( notion ) {
143+ const wrap = target => new Proxy ( target , {
144+ get ( obj , prop ) {
145+ const value = obj [ prop ] ;
146+ if ( typeof value === 'function' ) {
147+ return ( ...args ) => withRetry ( ( ) => value . apply ( obj , args ) ) ;
148+ }
149+ if ( value && typeof value === 'object' ) {
150+ return wrap ( value ) ;
151+ }
152+ return value ;
153+ } ,
154+ } ) ;
155+ return wrap ( notion ) ;
156+ }
157+
158+ function createNotionClient ( apiKey ) {
159+ const notion = new Client ( { auth : apiKey } ) ;
160+ return wrapNotionClient ( notion ) ;
161+ }
162+
140163function getNotion ( ) {
141164 if ( ! _notion ) {
142165 _notion = new Client ( { auth : getApiKey ( ) } ) ;
166+ _notionWithRetry = wrapNotionClient ( _notion ) ;
143167 }
144- return _notion ;
168+ return _notionWithRetry ;
145169}
146170
147171// ─── Helpers (imported from lib/helpers.js) ────────────────────────────────────
@@ -164,6 +188,8 @@ const {
164188 extractDynamicProps,
165189 UUID_REGEX ,
166190 paginate,
191+ withRetry,
192+ getNotionApiErrorDetails,
167193} = helpers ;
168194
169195/** Check if --json flag is set anywhere in the command chain */
@@ -184,7 +210,21 @@ async function runCommand(name, fn) {
184210 try {
185211 await fn ( ) ;
186212 } catch ( err ) {
187- console . error ( `${ name } failed:` , err . message ) ;
213+ const details = getNotionApiErrorDetails ( err ) ;
214+ if ( details ) {
215+ console . error ( `${ name } failed: Notion API error` ) ;
216+ if ( details . status !== undefined ) console . error ( `Status: ${ details . status } ` ) ;
217+ if ( details . code ) console . error ( `Code: ${ details . code } ` ) ;
218+ if ( details . message ) console . error ( `Message: ${ details . message } ` ) ;
219+ if ( details . body ) {
220+ const bodyText = typeof details . body === 'string'
221+ ? details . body
222+ : JSON . stringify ( details . body , null , 2 ) ;
223+ console . error ( `Body: ${ bodyText } ` ) ;
224+ }
225+ } else {
226+ console . error ( `${ name } failed:` , err . message ) ;
227+ }
188228 process . exit ( 1 ) ;
189229 }
190230}
@@ -300,7 +340,7 @@ program
300340 console . log ( '' ) ;
301341
302342 // Discover databases
303- const notion = new Client ( { auth : apiKey } ) ;
343+ const notion = createNotionClient ( apiKey ) ;
304344 try {
305345 const res = await notion . search ( {
306346 filter : { value : 'data_source' , property : 'object' } ,
0 commit comments