@@ -6,7 +6,7 @@ import SQLiteConnector from './dataConnectors/sqlite.js';
66import CodeInjector from './modules/codeInjector.js' ;
77import ExpressServer from './servers/express.js' ;
88// import FastifyServer from './servers/fastify.js';
9- import { ADMINFORTH_VERSION , listify , suggestIfTypo , RateLimiter , RAMLock , getClientIp , isProbablyUUIDColumn , convertPeriodToSeconds } from './modules/utils.js' ;
9+ import { ADMINFORTH_VERSION , listify , suggestIfTypo , RateLimiter , RAMLock , getClientIp , isProbablyUUIDColumn , convertPeriodToSeconds , hookResponseError } from './modules/utils.js' ;
1010import {
1111 type AdminForthConfig ,
1212 type IAdminForth ,
@@ -643,12 +643,9 @@ class AdminForth implements IAdminForth {
643643 extra,
644644 } ) ;
645645
646- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
647- throw new Error ( `Hook afterSave must return object with {ok: true} or { error: 'Error' } ` ) ;
648- }
649-
650- if ( resp . error ) {
651- return { error : resp . error } ;
646+ const hookRespError = hookResponseError ( resp ) ;
647+ if ( hookRespError ) {
648+ return hookRespError ;
652649 }
653650 }
654651
@@ -695,14 +692,9 @@ class AdminForth implements IAdminForth {
695692 response,
696693 extra,
697694 } ) ;
698- if ( ! resp || typeof resp . ok !== 'boolean' ) {
699- throw new Error ( `Hook beforeSave must return { ok: boolean, error?: string | null }` ) ;
700- }
701- if ( resp . ok === false && ! resp . error ) {
702- return { error : resp . error ?? 'Operation aborted by hook' } ;
703- }
704- if ( resp . error ) {
705- return { error : resp . error } ;
695+ const hookRespError = hookResponseError ( resp ) ;
696+ if ( hookRespError ) {
697+ return hookRespError ;
706698 }
707699 }
708700
@@ -740,11 +732,9 @@ class AdminForth implements IAdminForth {
740732 response,
741733 extra,
742734 } ) ;
743- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
744- throw new Error ( `Hook afterSave must return object with {ok: true} or { error: 'Error' } ` ) ;
745- }
746- if ( resp . error ) {
747- return { error : resp . error } ;
735+ const hookRespError = hookResponseError ( resp ) ;
736+ if ( hookRespError ) {
737+ return hookRespError ;
748738 }
749739 }
750740
@@ -771,12 +761,9 @@ class AdminForth implements IAdminForth {
771761 response,
772762 extra,
773763 } ) ;
774- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
775- throw new Error ( `Hook beforeSave must return object with {ok: true} or { error: 'Error' } ` ) ;
776- }
777-
778- if ( resp . error ) {
779- return { error : resp . error } ;
764+ const hookRespError = hookResponseError ( resp ) ;
765+ if ( hookRespError ) {
766+ return hookRespError ;
780767 }
781768 }
782769
@@ -794,12 +781,9 @@ class AdminForth implements IAdminForth {
794781 response,
795782 extra,
796783 } ) ;
797- if ( ! resp || ( ! resp . ok && ! resp . error ) ) {
798- throw new Error ( `Hook afterSave must return object with {ok: true} or { error: 'Error' } ` ) ;
799- }
800-
801- if ( resp . error ) {
802- return { error : resp . error } ;
784+ const hookRespError = hookResponseError ( resp ) ;
785+ if ( hookRespError ) {
786+ return hookRespError ;
803787 }
804788 }
805789
0 commit comments