-
Notifications
You must be signed in to change notification settings - Fork 9
Completed error logging to Audit (D990) #589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d520667
Completed error logging to Audit
droberts-ctrlo 612a083
Update to use `Log::Report`
droberts-ctrlo 28c2e7f
Change code as per review
droberts-ctrlo 6bcca4c
Fix erroring test
droberts-ctrlo 5c93275
Return JSON as required
droberts-ctrlo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| import { uploadMessage } from "./lib/MessageUploader"; | ||
|
|
||
| const createErrorString = (message: string, source: any, lineno: number, colno: number, error: Error | string | null) => { | ||
| let errorString = `Error: ${message}\nSource: ${source}\nLine: ${lineno}, Column: ${colno}`; | ||
| if (error && (error as Error)?.stack) { | ||
| errorString += `\nStack: ${(error as Error)?.stack}`; | ||
| } | ||
| return errorString; | ||
| } | ||
|
|
||
| window.onerror = function (message: string, source: any, lineno: number, colno: number, error: Error | string | null) { | ||
| if (location.host === 'localhost') { | ||
| // If we're on localhost, we log the error to the console. This is useful for development. | ||
| console.error("Script error occurred:", message, source, lineno, colno, error); | ||
| } | ||
| if (location.pathname === '/api/script_error' || location.pathname === '/login') { | ||
| // If we're on the script error page, we don't want to log it again. | ||
| console.error("Script error occurred but not logged to avoid recursion."); | ||
| console.error(createErrorString(message, source, lineno, colno, error)); | ||
| return; | ||
| } | ||
|
|
||
| const description = createErrorString(message, source, lineno, colno, error) | ||
|
|
||
| uploadMessage(description) | ||
| .catch(err => { | ||
| console.error("Failed to upload script error:", err); | ||
| }); | ||
| } | ||
|
|
||
| export { uploadMessage }; |
23 changes: 23 additions & 0 deletions
23
src/frontend/js/lib/util/scriptErrorHandler/lib/MessageUploader.test.ts
This file contains hidden or 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,23 @@ | ||
| import { describe, it, expect, jest } from '@jest/globals'; | ||
| import { MessageUploader } from './MessageUploader'; | ||
|
|
||
| describe('MessageUploader', () => { | ||
| class MockUploader { | ||
| upload = jest.fn(); | ||
| } | ||
|
|
||
| it('should upload messages correctly', async () => { | ||
| const uploader = new MockUploader(); | ||
| const messageUploader = new MessageUploader(uploader); | ||
|
|
||
| const messages = { id: 1, content: 'Test message 1' }; | ||
|
|
||
| await messageUploader.uploadMessage(JSON.stringify(messages)); | ||
|
|
||
| expect(uploader.upload).toHaveBeenCalledTimes(1); | ||
| expect(uploader.upload).toHaveBeenCalledWith({ | ||
| description: JSON.stringify(messages), | ||
| url: 'http://localhost/' | ||
| }); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
src/frontend/js/lib/util/scriptErrorHandler/lib/MessageUploader.ts
This file contains hidden or 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,30 @@ | ||
| import { Uploader } from "util/upload/UploadControl"; | ||
|
|
||
| export const uploadMessage = async (message: string) => { | ||
| const body = { | ||
| description: message, | ||
| url: window.location.href | ||
| }; | ||
| const uploader = new Uploader('/api/script_error', 'POST'); | ||
| const messageUploader = new MessageUploader(uploader); | ||
| return await messageUploader.uploadMessage(body.description); | ||
| }; | ||
|
|
||
| export class MessageUploader { | ||
| constructor(private uploader: Uploader) { | ||
| } | ||
|
|
||
| async uploadMessage(description: string): Promise<void> { | ||
| const csrf_token = document.body.dataset.csrf; | ||
| const body = { | ||
| description, | ||
| url: window.location.href, | ||
| csrf_token | ||
| }; | ||
| try { | ||
| return await this.uploader.upload(body); | ||
| } catch (err) { | ||
| console.error("Failed to upload message:", err); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.