-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move error handling code to error and debug
- Loading branch information
Showing
7 changed files
with
84 additions
and
80 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,36 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
|
||
import { getRandomUUID } from '../../shared/helpers/getRandomUUID'; | ||
import { UploadedFile } from '../storage/types'; | ||
|
||
export function perserveFilesForDebugging( | ||
uploadedFiles: UploadedFile[], | ||
err: Error | ||
) { | ||
const debugDirectory = path.join(os.tmpdir(), 'debug', getRandomUUID()); | ||
|
||
try { | ||
if (!fs.existsSync(debugDirectory)) { | ||
fs.mkdirSync(debugDirectory, { recursive: true }); | ||
console.log(`Created debug directory: ${debugDirectory}`); | ||
} | ||
|
||
const timestamp = new Date().toISOString(); | ||
const errorMessage = `${timestamp} - ${err.name}: \n${err.message}\n${err.stack}`; | ||
fs.writeFileSync(`${debugDirectory}/error.txt`, errorMessage); | ||
|
||
uploadedFiles.forEach((file, index) => { | ||
const destPath = path.join( | ||
debugDirectory, | ||
`${index}-${path.basename(file.originalname)}` | ||
); | ||
const fileContents = fs.readFileSync(file.path); | ||
fs.writeFileSync(destPath, fileContents); | ||
console.log(`Copied file ${file.path} to ${destPath}`); | ||
}); | ||
} catch (error) { | ||
console.error(`Error in perserveFilesForDebugging: ${error}`); | ||
} | ||
} |
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,19 @@ | ||
import { renderToStaticMarkup } from 'react-dom/server'; | ||
|
||
export const NO_PACKAGE_ERROR = new Error( | ||
renderToStaticMarkup( | ||
<> | ||
<div className="info"> | ||
Could not create a deck using your file(s) and rules. Make sure to at | ||
least create on valid toggle or verify your{' '} | ||
<a href="/upload?view=template">settings</a>? Example: | ||
</div> | ||
<div className="card" style={{ width: '50%', padding: '1rem' }}> | ||
<details> | ||
<summary>This the front</summary> | ||
This is the back | ||
</details> | ||
</div> | ||
</> | ||
) | ||
); |
This file was deleted.
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,25 @@ | ||
import express from 'express'; | ||
import { sendError } from '../../lib/error/sendError'; | ||
import { UploadedFile } from '../../lib/storage/types'; | ||
import { isLimitError } from '../../lib/misc/isLimitError'; | ||
import { isEmptyPayload } from '../../lib/misc/isEmptyPayload'; | ||
import { perserveFilesForDebugging } from '../../lib/debug/perserveFilesForDebugging'; | ||
|
||
export default function ErrorHandler( | ||
res: express.Response, | ||
req: express.Request, | ||
err: Error | ||
) { | ||
const uploadedFiles = req.files as UploadedFile[]; | ||
const skipError = isLimitError(err); | ||
|
||
if (!skipError) { | ||
sendError(err); | ||
if (!isEmptyPayload(uploadedFiles)) { | ||
perserveFilesForDebugging(req.files as UploadedFile[], err); | ||
} | ||
} | ||
|
||
res.set('Content-Type', 'text/plain'); | ||
res.status(400).send(err.message); | ||
} |
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