Remove unnecessary check for docker status code

This commit is contained in:
Felipe Lalanne 2022-09-14 10:10:40 -03:00
parent c6f911c36b
commit f7bc30a310
2 changed files with 10 additions and 26 deletions

View File

@ -17,6 +17,7 @@ import {
InternalInconsistencyError, InternalInconsistencyError,
NotFoundError, NotFoundError,
StatusCodeError, StatusCodeError,
isStatusError,
} from '../lib/errors'; } from '../lib/errors';
import * as LogTypes from '../lib/log-types'; import * as LogTypes from '../lib/log-types';
import { checkInt, isValidDeviceName } from '../lib/validation'; import { checkInt, isValidDeviceName } from '../lib/validation';
@ -312,51 +313,31 @@ export async function start(service: Service) {
reportNewStatus(containerId, service, 'Starting' as ServiceStatus); reportNewStatus(containerId, service, 'Starting' as ServiceStatus);
let shouldRemove = false;
let err: Error | undefined;
try { try {
await container.start(); await container.start();
} catch (e) { } catch (e) {
// Get the statusCode from the original cause and make sure it's if (!isStatusError(e)) {
// definitely an int for comparison reasons throw e;
// QUESTION: does this ever happen?
const maybeStatusCode = PermissiveNumber.decode(e.statusCode);
if (isLeft(maybeStatusCode)) {
shouldRemove = true;
err = new Error(`Could not parse status code from docker error: ${e}`);
throw err;
} }
const statusCode = maybeStatusCode.right;
const message = e.message; const { statusCode, message } = e;
// 304 means the container was already started, precisely what we want // 304 means the container was already started, precisely what we want
if (statusCode === 304) { if (statusCode === 304) {
alreadyStarted = true; alreadyStarted = true;
} else if ( } else if (
statusCode === 500 && statusCode === 500 &&
_.isString(message) &&
message.trim().match(/exec format error$/) message.trim().match(/exec format error$/)
) { ) {
// Provide a friendlier error message for "exec format error" // Provide a friendlier error message for "exec format error"
const deviceType = await config.get('deviceType'); const deviceType = await config.get('deviceType');
err = new Error( throw new Error(
`Application architecture incompatible with ${deviceType}: exec format error`, `Application architecture incompatible with ${deviceType}: exec format error`,
); );
throw err;
} else { } else {
// rethrow the same error // rethrow the top-level error
err = e;
throw e; throw e;
} }
} finally {
if (shouldRemove) {
// If starting the container fialed, we remove it so that it doesn't litter
await container.remove({ v: true }).catch(_.noop);
logger.logSystemEvent(LogTypes.startServiceError, {
service,
error: err,
});
}
} }
const serviceId = service.serviceId; const serviceId = service.serviceId;

View File

@ -19,6 +19,9 @@ export class StatusError extends Error {
} }
} }
export const isStatusError = (x: unknown): x is StatusError =>
x != null && x instanceof Error && !isNaN((x as any).statusCode);
interface CodedSysError extends Error { interface CodedSysError extends Error {
code?: string; code?: string;
} }