Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 8a4bc83

Browse files
committed
refactor: Use ServerlessError in pip
1 parent 618ef76 commit 8a4bc83

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/pip.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function generateRequirementsFile(
107107
}
108108
}
109109

110-
async function pipAcceptsSystem(pythonBin) {
110+
async function pipAcceptsSystem(pythonBin, pluginInstance) {
111111
// Check if pip has Debian's --system option and set it if so
112112
try {
113113
const pipTestRes = await spawn(pythonBin, ['-m', 'pip', 'help', 'install']);
@@ -120,7 +120,10 @@ async function pipAcceptsSystem(pythonBin) {
120120
e.stderrBuffer &&
121121
e.stderrBuffer.toString().includes('command not found')
122122
) {
123-
throw new Error(`${pythonBin} not found! Try the pythonBin option.`);
123+
throw new pluginInstance.serverless.classes.Error(
124+
`${pythonBin} not found! Install it according to the poetry docs.`,
125+
'PYTHON_REQUIREMENTS_PYTHON_NOT_FOUND'
126+
);
124127
}
125128
throw e;
126129
}
@@ -167,10 +170,9 @@ async function installRequirements(targetFolder, pluginInstance) {
167170
// Check if we're using the legacy --cache-dir command...
168171
if (options.pipCmdExtraArgs.indexOf('--cache-dir') > -1) {
169172
if (options.dockerizePip) {
170-
throw (
171-
'Error: You can not use --cache-dir with Docker any more, please\n' +
172-
' use the new option useDownloadCache instead. Please see:\n' +
173-
' https://github.com/UnitedIncome/serverless-python-requirements#caching'
173+
throw new pluginInstance.serverless.classes.Error(
174+
'You cannot use --cache-dir with Docker any more, please use the new option useDownloadCache instead. Please see: https://github.com/UnitedIncome/serverless-python-requirements#caching for more details.',
175+
'PYTHON_REQUIREMENTS_CACHE_DIR_DOCKER_INVALID'
174176
);
175177
} else {
176178
if (log) {
@@ -222,7 +224,7 @@ async function installRequirements(targetFolder, pluginInstance) {
222224
pipCmd.push('--cache-dir', downloadCacheDir);
223225
}
224226

225-
if (await pipAcceptsSystem(options.pythonBin)) {
227+
if (await pipAcceptsSystem(options.pythonBin, pluginInstance)) {
226228
pipCmd.push('--system');
227229
}
228230
}
@@ -358,7 +360,10 @@ async function installRequirements(targetFolder, pluginInstance) {
358360
if (Array.isArray(options.dockerRunCmdExtraArgs)) {
359361
dockerCmd.push(...options.dockerRunCmdExtraArgs);
360362
} else {
361-
throw new Error('dockerRunCmdExtraArgs option must be an array');
363+
throw new pluginInstance.serverless.classes.Error(
364+
'dockerRunCmdExtraArgs option must be an array',
365+
'PYTHON_REQUIREMENTS_INVALID_DOCKER_EXTRA_ARGS'
366+
);
362367
}
363368

364369
dockerCmd.push(dockerImage);
@@ -405,7 +410,10 @@ async function installRequirements(targetFolder, pluginInstance) {
405410
cmd.indexOf('python') > -1
406411
? 'Try the pythonBin option'
407412
: 'Please install it';
408-
throw new Error(`${cmd} not found! ${advice}`);
413+
throw new pluginInstance.serverless.classes.Error(
414+
`${cmd} not found! ${advice}`,
415+
'PYTHON_REQUIREMENTS_COMMAND_NOT_FOUND'
416+
);
409417
}
410418
throw e;
411419
}

0 commit comments

Comments
 (0)