Skip to content

Commit

Permalink
[OGUI-1594] Remove double conversion of grpc error and adapt to usage…
Browse files Browse the repository at this point in the history
… of errors from web-ui (#2713)

* removes CustomError Classes from Control codebase and reuses them from web-ui
* fixes a bug in which EnvironmentService was trying to convert an error from a gRPC error
  • Loading branch information
graduta authored Jan 30, 2025
1 parent 4b0b689 commit 52751c1
Show file tree
Hide file tree
Showing 30 changed files with 223 additions and 275 deletions.
2 changes: 1 addition & 1 deletion Control/lib/control-core/GrpcProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
const protoLoader = require('@grpc/proto-loader');
const grpcLibrary = require('@grpc/grpc-js');
const path = require('path');
const {grpcErrorToNativeError} = require('./../errors/grpcErrorToNativeError.js');
const {grpcErrorToNativeError} = require('@aliceo2/web-ui');
const {Status} = require(path.join(__dirname, './../../protobuf/status_pb.js'));
const {EnvironmentInfo} = require(path.join(__dirname, './../../protobuf/environmentinfo_pb.js'));
const logger = (require('@aliceo2/web-ui').LogManager)
Expand Down
45 changes: 27 additions & 18 deletions Control/lib/controllers/Environment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* or submit itself to any jurisdiction.
*/
const {LogManager, LogLevel} = require('@aliceo2/web-ui');
const {
updateAndSendExpressResponseFromNativeError, grpcErrorToNativeError, InvalidInputError, UnauthorizedAccessError
} = require('@aliceo2/web-ui');

const LOG_FACILITY = 'cog/env-ctrl';
const {EnvironmentTransitionType} = require('./../common/environmentTransitionType.enum.js');
const {grpcErrorToNativeError} = require('./../errors/grpcErrorToNativeError.js');
const {InvalidInputError} = require('./../errors/InvalidInputError.js');
const {UnauthorizedAccessError} = require('./../errors/UnauthorizedAccessError.js');
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
const {User} = require('./../dtos/User.js');

/**
Expand Down Expand Up @@ -64,15 +64,15 @@ class EnvironmentController {
async getEnvironmentHandler(req, res) {
const {id, source} = req.params;
if (!id) {
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
return;
}
try {
const response = await this._envService.getEnvironment(id, source);
res.status(200).json(response);
} catch (error) {
this._logger.debug(error);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand All @@ -88,9 +88,12 @@ class EnvironmentController {
const {id} = req.params;
const {type: transitionType, runNumber = ''} = req.body;
if (!id) {
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
} else if (!(transitionType in EnvironmentTransitionType)) {
updateExpressResponseFromNativeError(res, new InvalidInputError('Invalid environment transition to perform'));
updateAndSendExpressResponseFromNativeError(
res,
new InvalidInputError('Invalid environment transition to perform'),
);
} else {
const transitionRequestedAt = Date.now();
let response = null;
Expand All @@ -105,7 +108,7 @@ class EnvironmentController {
`Request to transition environment by ${req.session.username} to ${transitionType} failed due to ${error}`,
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
const currentRunNumber = response?.currentRunNumber ?? runNumber;
this._logger.debug(`${transitionType},${id},${currentRunNumber},${transitionRequestedAt},${Date.now()}`);
Expand All @@ -125,7 +128,7 @@ class EnvironmentController {
const {runNumber = '', keepTasks = false, allowInRunningState = false, force = false} = req.body ?? {};

if (!id) {
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing environment ID parameter'));
} else {
const destroyRequestedAt = Date.now();
this._logger.infoMessage(`Request to destroy environment by ${req.session.username} by force: ${force}`,
Expand All @@ -139,7 +142,7 @@ class EnvironmentController {
`Request to destroy environment by ${req.session.username} failed due to ${error}`,
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY, partition: id, run: runNumber}
);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
this._logger.debug(`DESTROY_ENVIRONMENT,${id},${runNumber},${destroyRequestedAt},${Date.now()}`);
}
Expand All @@ -157,23 +160,29 @@ class EnvironmentController {
const {detector, runType, configurationName} = req.body;

if (!this._lockService.isLockOwnedByUser(detector, user)) {
updateExpressResponseFromNativeError(res, new UnauthorizedAccessError('Lock not taken'));
updateAndSendExpressResponseFromNativeError(res, new UnauthorizedAccessError('Lock not taken'));
return;
}

if (!configurationName) {
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing Configuration Name for deployment'));
updateAndSendExpressResponseFromNativeError(
res,
new InvalidInputError('Missing Configuration Name for deployment')
);
return;
}

try {
const areDetectorsAvailable = await this._detectorService.areDetectorsAvailable([detector]);
if (!areDetectorsAvailable) {
updateExpressResponseFromNativeError(res, new InvalidInputError(`Detector ${detector} is already active`));
updateAndSendExpressResponseFromNativeError(
res,
new InvalidInputError(`Detector ${detector} is already active`)
);
return;
}
} catch (error) {
updateExpressResponseFromNativeError(res, grpcErrorToNativeError(error));
updateAndSendExpressResponseFromNativeError(res, grpcErrorToNativeError(error));
return;
}

Expand All @@ -188,7 +197,7 @@ class EnvironmentController {
} catch (error) {
this._logger.debug(`Unable to retrieve saved configuration for ${configurationName} due to`);
this._logger.debug(error);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
return;
}

Expand All @@ -199,7 +208,7 @@ class EnvironmentController {
workflowTemplatePath = `${repository}/workflows/${template}@${revision}`;
} catch (error) {
this._logger.debug(`Unable to retrieve default workflow template due to ${error}`);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
return;
}
// Attempt to deploy environment
Expand All @@ -216,7 +225,7 @@ class EnvironmentController {
`Unable to deploy request by username(${username}) for ${configurationName} due to error`,
{level: LogLevel.OPERATIONS, system: 'GUI', facility: LOG_FACILITY}
);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Control/lib/controllers/Lock.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* or submit itself to any jurisdiction.
*/

const {InvalidInputError} = require('./../errors/InvalidInputError.js');
const {DetectorLockAction} = require('./../common/lock/detectorLockAction.enum.js');
const {LogManager, LogLevel} = require('@aliceo2/web-ui');
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
const { LogManager, LogLevel } = require('@aliceo2/web-ui');
const { updateAndSendExpressResponseFromNativeError, InvalidInputError } = require('@aliceo2/web-ui');

const { DetectorLockAction } = require('./../common/lock/detectorLockAction.enum.js');
const {User} = require('./../dtos/User.js');

const LOG_FACILITY = 'cog/log-ctrl';
Expand Down Expand Up @@ -48,7 +48,7 @@ class LockController {
try {
res.status(200).json(this._lockService.locksByDetectorToJSON());
} catch (error) {
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ class LockController {
}
} catch (error) {
this._logger.errorMessage(error, {level: LogLevel.DEVELOPER, facility: LOG_FACILITY});
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand Down
7 changes: 3 additions & 4 deletions Control/lib/controllers/Run.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const {LogManager, LogLevel} = require('@aliceo2/web-ui');
const {LogManager, LogLevel, updateAndSendExpressResponseFromNativeError} = require('@aliceo2/web-ui');
const LOG_FACILITY = 'run-ctrl';
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
const {CacheKeys} = require('./../common/cacheKeys.enum.js');

/**
Expand Down Expand Up @@ -59,7 +58,7 @@ class RunController {
res.status(200).json(calibrationRuns);
} catch (error) {
this._logger.debug(error);
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand All @@ -81,7 +80,7 @@ class RunController {
const logMessage = `Error refreshing calibration configuration by ${req.session.username} due to: ${error}`;
this._logger.errorMessage(logMessage, {level: LogLevel.OPERATIONS, facility: LOG_FACILITY})

updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions Control/lib/controllers/WorkflowTemplate.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const {updateExpressResponseFromNativeError} = require('./../errors/updateExpressResponseFromNativeError.js');
const {updateAndSendExpressResponseFromNativeError, InvalidInputError} = require('@aliceo2/web-ui');

/**
* Controller for dealing with all API requests on workflow templates from AliECS:
Expand Down Expand Up @@ -40,7 +40,7 @@ class WorkflowTemplateController {
const defaultTemplateSource = await this._workflowService.getDefaultTemplateSource();
res.status(200).json(defaultTemplateSource);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand All @@ -55,7 +55,7 @@ class WorkflowTemplateController {
const mappings = await this._workflowService.retrieveWorkflowMappings();
res.status(200).json(mappings);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}

Expand All @@ -69,13 +69,16 @@ class WorkflowTemplateController {
try {
const {name} = req.query;
if (!name) {
res.status(400).json({message: 'No name for the configuration provided'});
updateAndSendExpressResponseFromNativeError(
res,
new InvalidInputError('No name for the configuration provided')
);
return;
}
const mappings = await this._workflowService.retrieveWorkflowSavedConfiguration(name);
res.status(200).json(mappings);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
updateAndSendExpressResponseFromNativeError(res, error);
}
}
}
Expand Down
19 changes: 0 additions & 19 deletions Control/lib/errors/InvalidInputError.js

This file was deleted.

19 changes: 0 additions & 19 deletions Control/lib/errors/NotFoundError.js

This file was deleted.

19 changes: 0 additions & 19 deletions Control/lib/errors/TimeoutError.js

This file was deleted.

19 changes: 0 additions & 19 deletions Control/lib/errors/UnauthorizedAccessError.js

This file was deleted.

39 changes: 0 additions & 39 deletions Control/lib/errors/grpcErrorToNativeError.js

This file was deleted.

Loading

0 comments on commit 52751c1

Please sign in to comment.