@@ -93,6 +93,19 @@ export function usePersistedOperations<
9393 const operationASTByRequest = new WeakMap < Request , DocumentNode > ( )
9494 const persistedOperationRequest = new WeakSet < Request > ( )
9595
96+ const notFoundErrorFactory = createErrorFactory (
97+ 'PersistedQueryNotFound' ,
98+ customErrors ?. notFound ,
99+ )
100+ const keyNotFoundErrorFactory = createErrorFactory (
101+ 'PersistedQueryKeyNotFound' ,
102+ customErrors ?. keyNotFound ,
103+ )
104+ const persistentQueryOnlyErrorFactory = createErrorFactory (
105+ 'PersistedQueryOnly' ,
106+ customErrors ?. persistedQueryOnly ,
107+ )
108+
96109 return {
97110 async onParams ( payload ) {
98111 const { request, params, setParams } = payload
@@ -103,32 +116,20 @@ export function usePersistedOperations<
103116 ? allowArbitraryOperations
104117 : await allowArbitraryOperations ( request ) ) === false
105118 ) {
106- throw createPersistedOperationError (
107- 'PersistedQueryOnly' ,
108- payload ,
109- customErrors ?. persistedQueryOnly ,
110- )
119+ throw persistentQueryOnlyErrorFactory ( payload )
111120 }
112121 return
113122 }
114123
115124 const persistedOperationKey = extractPersistedOperationId ( params )
116125
117126 if ( persistedOperationKey == null ) {
118- throw createPersistedOperationError (
119- 'PersistedQueryNotFound' ,
120- payload ,
121- customErrors ?. keyNotFound ,
122- )
127+ throw keyNotFoundErrorFactory ( payload )
123128 }
124129
125130 const persistedQuery = await getPersistedOperation ( persistedOperationKey )
126131 if ( persistedQuery == null ) {
127- throw createPersistedOperationError (
128- 'PersistedQueryNotFound' ,
129- payload ,
130- customErrors ?. notFound ,
131- )
132+ throw notFoundErrorFactory ( payload )
132133 }
133134
134135 if ( typeof persistedQuery === 'object' ) {
@@ -161,16 +162,19 @@ export function usePersistedOperations<
161162 }
162163}
163164
164- function createPersistedOperationError (
165+ function createErrorFactory (
165166 defaultMessage : string ,
166- payload : OnParamsEventPayload ,
167167 options ?: CustomErrorFactory ,
168168) {
169169 if ( typeof options === 'string' ) {
170- return createGraphQLError ( options )
170+ return ( ) => createGraphQLError ( options )
171171 }
172+
172173 if ( typeof options === 'function' ) {
173- return options ( payload )
174+ return options
175+ }
176+
177+ return ( ) => {
178+ return createGraphQLError ( options ?. message ?? defaultMessage , options )
174179 }
175- return createGraphQLError ( options ?. message ?? defaultMessage , options )
176180}
0 commit comments