-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Added Error Logging Apollo Link (#1832)
* Feat: Added Error Logging Apollo Link * Update errorLoggingLink.test.ts * Update errorLoggingLink.test.ts * Update errorLoggingLink.test.ts
- Loading branch information
Showing
7 changed files
with
246 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@faustwp/core': patch | ||
--- | ||
|
||
Implemented ErrorLoggingLink class to capture GraphQL errors and server errors, providing enhanced error handling and logging capabilities. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
ApolloLink, | ||
FetchResult, | ||
NextLink, | ||
Operation, | ||
ServerError, | ||
} from '@apollo/client'; | ||
import { Observable } from 'zen-observable-ts'; | ||
import { errorLog } from '../utils/log.js'; | ||
|
||
/** | ||
* Checks if the given error is a server error. | ||
* @param error The error to check. | ||
* @returns A boolean indicating whether the error is a server error. | ||
*/ | ||
function isServerError(error: unknown): error is ServerError { | ||
if ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'response' in error && | ||
'result' in error && | ||
'statusCode' in error | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Apollo Link that captures GraphQL errors and server errors, and prints them into the console. | ||
*/ | ||
export class ErrorLoggingLink extends ApolloLink { | ||
/** | ||
* Intercepts each GraphQL operation request. | ||
* @param operation The GraphQL operation being executed. | ||
* @param forward The next link in the chain to delegate the operation to. | ||
* @returns An Observable with the operation result or error. | ||
*/ | ||
// eslint-disable-next-line class-methods-use-this | ||
request( | ||
operation: Operation, | ||
forward: NextLink, | ||
): Observable<FetchResult> | null { | ||
return new Observable<FetchResult>((observer) => { | ||
const subscription = forward(operation).subscribe({ | ||
next: (result) => { | ||
// Check if there are GraphQL errors in the result | ||
if (result.errors && result.errors.length > 0) { | ||
errorLog('GraphQL errors:', result.errors); | ||
} | ||
observer.next(result); | ||
}, | ||
error: (error) => { | ||
// Check if the error is a server error | ||
if (isServerError(error)) { | ||
errorLog('Server error:', error); | ||
errorLog('Fetch result:', error.result); | ||
} else { | ||
errorLog('Network error:', error); | ||
} | ||
observer.error(error); | ||
}, | ||
complete: () => { | ||
observer.complete(); | ||
}, | ||
}); | ||
|
||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './errorLoggingLink.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.