-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Abort file processing when DB is not responsible (#289)
- Loading branch information
Showing
10 changed files
with
345 additions
and
214 deletions.
There are no files selected for viewing
402 changes: 192 additions & 210 deletions
402
src/server/controllers/item/create-item-controller.ts
Large diffs are not rendered by default.
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
17 changes: 17 additions & 0 deletions
17
src/server/controllers/item/shared/item-error-handler.spec.ts
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,17 @@ | ||
import { db } from "../../../../db/db" | ||
import { itemErrorHandler } from "./item-error-handler" | ||
|
||
jest.mock("../../../../db/db") | ||
|
||
describe("itemErrorHandler", () => { | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
it("should try set the item to error state", () => { | ||
(db.none as any).mockResolvedValueOnce(null) | ||
const spy = jest.spyOn(require("../../../queries/items"), "updateItem") | ||
itemErrorHandler("item_id", Error("Test Error")) | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
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,33 @@ | ||
import { DataStreamingToDatabaseException } from "../../../errors/data-streaming-to-database-exception" | ||
import { logger } from "../../../../logger" | ||
import { DataParsingException } from "../../../errors/data-parsing-exception" | ||
import { DataProcessingException } from "../../../errors/data-processing-exceptions" | ||
import { UnlinkingFileException } from "../../../errors/unlinking-file-exception" | ||
import { db } from "../../../../db/db" | ||
import { updateItem } from "../../../queries/items" | ||
import { ReportStatus } from "../../../queries/items.model" | ||
|
||
export const itemErrorHandler = async (itemId: string, processingError: Error) => { | ||
if (processingError instanceof DataStreamingToDatabaseException) { | ||
// eslint-disable-next-line max-len | ||
logger.error(`Error occurred during data parsing and saving into database: ${processingError.message}, item_id: ${itemId}`) | ||
} else if (processingError instanceof DataParsingException) { | ||
// eslint-disable-next-line max-len | ||
logger.error(`Error occurred during data parsing or manipulation: ${processingError.message}, item_id: ${itemId}`) | ||
} else if (processingError instanceof DataProcessingException) { | ||
// eslint-disable-next-line max-len | ||
logger.error(`Error occurred during samples aggregation in database: ${processingError.message}, item_id: ${itemId}`) | ||
} else if (processingError instanceof UnlinkingFileException) { | ||
// eslint-disable-next-line max-len | ||
logger.error(`Error occurred during deleting of uploaded file: ${processingError.message}, item_id: ${itemId}`) | ||
} else { | ||
logger.error(`Unexpected error occurred: ${processingError.message}, item_id: ${itemId}`) | ||
} | ||
|
||
try { | ||
logger.info(`Trying to set item: ${itemId} to error state.`) | ||
await db.none(updateItem(itemId, ReportStatus.Error, null)) | ||
} catch(e) { | ||
logger.error(`It was not possible to set item ${itemId} to error state: ${e}`) | ||
} | ||
} |
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,9 @@ | ||
export class DataParsingException extends Error { | ||
constructor (message) { | ||
super(message) | ||
|
||
this.name = this.constructor.name | ||
|
||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} |
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,9 @@ | ||
export class DataProcessingException extends Error { | ||
constructor (message) { | ||
super(message) | ||
|
||
this.name = this.constructor.name | ||
|
||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} |
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,9 @@ | ||
export class DataStreamingToDatabaseException extends Error { | ||
constructor (message) { | ||
super(message) | ||
|
||
this.name = this.constructor.name | ||
|
||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} |
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,9 @@ | ||
export class UnlinkingFileException extends Error { | ||
constructor (message) { | ||
super(message) | ||
|
||
this.name = this.constructor.name | ||
|
||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} |
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