Merge pull request #1863 from balena-io/remove-string-error-handling

Restrict error handler typing
This commit is contained in:
srlowe 2020-06-10 15:05:39 +02:00 committed by GitHub
commit 995f8a3338
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 23 deletions

View File

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { BalenaError } from 'balena-errors';
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import * as os from 'os';
@ -149,18 +150,12 @@ async function getSentry() {
return await import('@sentry/node');
}
export async function handleError(error: any) {
export async function handleError(error: Error) {
// Set appropriate exitCode
process.exitCode =
error.exitCode === 0
(error as BalenaError).exitCode === 0
? 0
: parseInt(error.exitCode, 10) || process.exitCode || 1;
// Handle non-Error objects (probably strings)
if (!(error instanceof Error)) {
ErrorsModule.printErrorMessage(String(error));
return;
}
: Math.trunc((error as BalenaError).exitCode) || process.exitCode || 1;
// Prepare message
const message = [interpret(error)];
@ -173,7 +168,7 @@ export async function handleError(error: any) {
const isExpectedError =
error instanceof ExpectedError ||
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
if (isExpectedError) {

View File

@ -59,19 +59,6 @@ describe('handleError() function', () => {
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 () => {
const errorMessage = 'an expected error';
const error = new ErrorsModule.ExpectedError(errorMessage);