-
Notifications
You must be signed in to change notification settings - Fork 144
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
Improved error handling #178
base: main
Are you sure you want to change the base?
Changes from 4 commits
613ae36
c08460a
aa46eba
7825c2f
f48a45c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const messages = require('./messages') | ||
|
||
// For now, this should just throw for things that would stop the app from booting. | ||
|
||
module.exports = () => { | ||
const { errorMessages } = messages | ||
const { APPROVED_DOMAINS, DRIVE_TYPE, DRIVE_ID } = process.env | ||
const errors = [] | ||
|
||
if (!APPROVED_DOMAINS) errors.push(errorMessages.noApprovedDomains) | ||
if (!DRIVE_TYPE) errors.push(errorMessages.noDriveType) | ||
if (!DRIVE_ID) errors.push(errorMessages.noDriveID) | ||
|
||
if (errors.length) { | ||
console.log('***********************************************') | ||
console.log('Your library instance has configuration issues:') | ||
errors.forEach((message) => console.error(` > ${message}`)) | ||
console.log('Address these issues and restart the app.') | ||
console.log('***********************************************') | ||
process.exit(1) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"errorMessages": { | ||
"noApprovedDomains": "You must set the APPROVED_DOMAINS environment variable to a list of domains or a regular expression.", | ||
"noDriveType": "No DRIVE_TYPE env variable set. Please set it to 'team' or 'folder.'", | ||
"noDriveID": "No DRIVE_ID env variable set. Set this environment variable to the ID of your drive or folder and restart the app.", | ||
"noFilesFound": "No files found. Ensure your DRIVE_ID and DRIVE_TYPE environment variables are set correctly and that your service account's email is shared with your drive or folder.\n\nIf you just added the account, wait a minute and try again." | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ module.exports = async (err, req, res, next) => { | |
const inlined = await loadInlineAssets() | ||
res.status(code).render(`errors/${code}`, { | ||
inlineCSS: inlined.css, | ||
isDev: process.env.NODE_ENV === 'development', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
err, | ||
template: inlined.stringTemplate | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
const search = require('../search') | ||
const move = require('../move') | ||
const { getAuth } = require('../auth') | ||
const { errorMessages } = require('../errors/messages.json') | ||
|
||
const router = require('express-promise-router')() | ||
|
||
|
@@ -14,7 +16,7 @@ router.get('/:page', handlePage) | |
router.get('/filename-listing.json', async (req, res) => { | ||
res.header('Cache-Control', 'public, must-revalidate') // override no-cache | ||
const filenames = await getFilenames() | ||
res.json({filenames: filenames}) | ||
res.json({ filenames: filenames }) | ||
}) | ||
|
||
module.exports = router | ||
|
@@ -57,6 +59,12 @@ async function handlePage(req, res) { | |
|
||
if (page === 'categories' || page === 'index') { | ||
const tree = await getTree() | ||
if (!tree.children) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This signal is great. It also seems like we could easily make this nonfatal (log, but have the library site be empty) if we wanted. |
||
// pull the auth client email to make debugging easier: | ||
const authClient = await getAuth() | ||
const errMsg = errorMessages.noFilesFound.replace('email', `email (<code>${authClient.email}</code>)`) | ||
throw new Error(errMsg) | ||
} | ||
const categories = buildDisplayCategories(tree) | ||
res.render(template, { ...categories, template: stringTemplate }) | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,8 +44,11 @@ exports.requireWithFallback = (attemptPath) => { | |
return require(customPath) | ||
} catch (err) { | ||
// if the file exists but we failed to pull it in, log that error at a warning level | ||
const level = fs.existsSync(customPath) ? 'warn' : 'debug' | ||
log[level](`Failed pulling in custom file ${attemptPath} @ ${customPath}. Error was:`, err) | ||
// with no stacktrace. | ||
const fileExists = fs.existsSync(customPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This also seems like a useful signal. Let's make sure it merges cleanly with the other open PR for this case, https://github.com/nytimes/library/pull/202/files |
||
const level = fileExists ? 'warn' : 'debug' | ||
const toLog = fileExists ? err : 'No override file given.' | ||
log[level](`Failed pulling in custom file ${attemptPath} @ ${customPath}. Error was:`, toLog) | ||
return require(serverPath) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Should we include the full trace here, too?