118
119 if (createError.isHttpError(err) && err.status < 500) {
120 res.status(err.status).json({ error: err.message })
121 return122 }
123 res.status(500).json({ error: 'Internal Server Error!' })
124}
112 return
113 default:
114 res.status(400).json({ error: 'Invalid form data!' })
115 return116 }
117 }
118
109 config.max_image_size_in_bytes / 1024 / 1024
110 } MB.`
111 })
112 return113 default:
114 res.status(400).json({ error: 'Invalid form data!' })
115 return
102 return
103 case 'LIMIT_FILE_COUNT':
104 res.status(400).json({ error: 'Too many files; limit is 1.' })
105 return106 case 'LIMIT_FILE_SIZE':
107 res.status(413).json({
108 error: `File too large; limit is ${
99 res.status(400).json({
100 error: `Unexpected field name ${err.field}; field name must be 'file'.`
101 })
102 return103 case 'LIMIT_FILE_COUNT':
104 res.status(400).json({ error: 'Too many files; limit is 1.' })
105 return
Any code paths that do not have explicit returns will return undefined
.
It is recommended to replace any implicit dead-ends that return undefined
with a return null
statement.
As a convention, undefined
signals that an unexpected value has been produced as the
result of a logical error in the program.
A null
return, on the other hand, signals that the input to a function was incorrect,
or a value was 'not found'.
Having this distinction in code helps you figure out if something was caused because of a logical error, or malformed input to a function call.
function getUser(name) {
if (userExists(name)) {
return userDb.get(name);
}
// whoops! Implicit undefined return
}
function readFile(fileName) {
if (fs.existsSync(fileName)) {
return fs.readFileSync(fileName, 'utf-8');
}
return; // Implicit undefined return.
}
function getUser(name) {
if (userExists(name)) {
return userDb.get(name);
}
// `null` indicates there is no such user.
return null;
}
function readFile(fileName) {
if (fs.existsSync(fileName)) {
return fs.readFileSync(fileName, 'utf-8');
}
// `null` indicates there is no such file.
return null;
}