mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-21 09:51:58 +00:00
Merge pull request #1863 from balena-io/remove-string-error-handling
Restrict error handler typing
This commit is contained in:
commit
995f8a3338
@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { BalenaError } from 'balena-errors';
|
||||||
import { stripIndent } from 'common-tags';
|
import { stripIndent } from 'common-tags';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
@ -149,18 +150,12 @@ async function getSentry() {
|
|||||||
return await import('@sentry/node');
|
return await import('@sentry/node');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleError(error: any) {
|
export async function handleError(error: Error) {
|
||||||
// Set appropriate exitCode
|
// Set appropriate exitCode
|
||||||
process.exitCode =
|
process.exitCode =
|
||||||
error.exitCode === 0
|
(error as BalenaError).exitCode === 0
|
||||||
? 0
|
? 0
|
||||||
: parseInt(error.exitCode, 10) || process.exitCode || 1;
|
: Math.trunc((error as BalenaError).exitCode) || process.exitCode || 1;
|
||||||
|
|
||||||
// Handle non-Error objects (probably strings)
|
|
||||||
if (!(error instanceof Error)) {
|
|
||||||
ErrorsModule.printErrorMessage(String(error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare message
|
// Prepare message
|
||||||
const message = [interpret(error)];
|
const message = [interpret(error)];
|
||||||
@ -173,7 +168,7 @@ export async function handleError(error: any) {
|
|||||||
const isExpectedError =
|
const isExpectedError =
|
||||||
error instanceof ExpectedError ||
|
error instanceof ExpectedError ||
|
||||||
EXPECTED_ERROR_REGEXES.some(re => re.test(message[0])) ||
|
EXPECTED_ERROR_REGEXES.some(re => re.test(message[0])) ||
|
||||||
EXPECTED_ERROR_REGEXES.some(re => re.test((error as any).code));
|
EXPECTED_ERROR_REGEXES.some(re => re.test((error as BalenaError).code));
|
||||||
|
|
||||||
// Output/report error
|
// Output/report error
|
||||||
if (isExpectedError) {
|
if (isExpectedError) {
|
||||||
|
@ -59,19 +59,6 @@ describe('handleError() function', () => {
|
|||||||
sandbox.restore();
|
sandbox.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call printErrorMessage and exit when passed a string', async () => {
|
|
||||||
const errorString = 'a string';
|
|
||||||
|
|
||||||
await ErrorsModule.handleError(errorString);
|
|
||||||
|
|
||||||
expect(printErrorMessage.calledOnce).to.be.true;
|
|
||||||
expect(printErrorMessage.getCall(0).args[0]).to.equal(errorString);
|
|
||||||
|
|
||||||
expect(printExpectedErrorMessage.notCalled).to.be.true;
|
|
||||||
expect(captureException.notCalled).to.be.true;
|
|
||||||
expect(processExit.notCalled).to.be.true;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should process ExpectedErrors as expected', async () => {
|
it('should process ExpectedErrors as expected', async () => {
|
||||||
const errorMessage = 'an expected error';
|
const errorMessage = 'an expected error';
|
||||||
const error = new ErrorsModule.ExpectedError(errorMessage);
|
const error = new ErrorsModule.ExpectedError(errorMessage);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user