Skip to content

Commit

Permalink
Merge pull request telefonicaid#1582 from telefonicaid/task/add_typeI…
Browse files Browse the repository at this point in the history
…nformation_in_errors

add device/group information to error log about some errors
  • Loading branch information
fgalan authored Feb 22, 2024
2 parents b1da0d0 + 09b7ebc commit 46fbcb2
Show file tree
Hide file tree
Showing 22 changed files with 237 additions and 134 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add: log and return device/group information when EntityGenericError, TypeNotFound, DeviceNotFound, BadTimestamp, BadGeocoordinates, CommandNotFound, GroupNotFound, MissingAttributes, DuplicateDeviceId and DuplicateGroup errors (iotagent-json#815)
- Hardening: simplify implementation so typeInformation contains global config values (#1515)
- Add: `POST /iot/op/delete` operation to delete multiple devices at once (#1578)
- Fix: store device subscriptions updates (#1086)
Expand Down
65 changes: 45 additions & 20 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ class UnregistrationError {
}
}
class EntityGenericError {
constructor(id, type, details, code) {
constructor(id, type, typeInformation, details, code) {
this.name = 'ENTITY_GENERIC_ERROR';
this.message = 'Error accesing entity data for device: ' + id + ' of type: ' + type;
this.message =
'Error accesing entity data for device: ' +
id +
' of type: ' +
type +
' and ' +
JSON.stringify(typeInformation);
this.details = details || {};
this.code = code || 200;
}
Expand Down Expand Up @@ -77,36 +83,44 @@ class UnsupportedContentType {
}
}
class TypeNotFound {
constructor(id, type) {
constructor(id, type, typeInformation) {
this.name = 'TYPE_NOT_FOUND';
this.message = 'Type : ' + type + ' not found for device with id: ' + id;
this.message =
'Type : ' + type + ' not found for device with id: ' + id + ' with: ' + JSON.stringify(typeInformation);
this.code = 500;
}
}
class MissingAttributes {
constructor(msg) {
constructor(msg, device) {
this.name = 'MISSING_ATTRIBUTES';
this.message = 'The request was not well formed:' + msg;
this.message = 'The request was not well formed:' + msg + ' with: ' + JSON.stringify(device);
}
}
class DeviceNotFound {
constructor(id) {
constructor(id, typeInformation) {
this.name = 'DEVICE_NOT_FOUND';
this.message = 'No device was found with id:' + id;
this.message = 'No device was found with id:' + id + ' and ' + JSON.stringify(typeInformation);
this.code = 404;
}
}
class DuplicateDeviceId {
constructor(id) {
constructor(device) {
this.name = 'DUPLICATE_DEVICE_ID';
this.message = 'A device with the same pair (Service, DeviceId) was found:' + id;
this.message =
'A device with the same pair (Service, DeviceId) was found:' + device.id + ' and ' + JSON.stringify(device);
this.code = 409;
}
}
class DuplicateGroup {
constructor(res, key) {
constructor(group) {
this.name = 'DUPLICATE_GROUP';
this.message = 'A device configuration already exists for resource ' + res + ' and API Key ' + key;
this.message =
'A device configuration already exists for resource ' +
group.resource +
' and API Key ' +
group.apikey +
' and ' +
JSON.stringify(group);
this.code = 409;
}
}
Expand Down Expand Up @@ -170,9 +184,13 @@ class WrongSyntax {
}
}
class CommandNotFound {
constructor(name) {
constructor(name, typeInformation) {
this.name = 'COMMAND_NOT_FOUND';
this.message = "Couldn't update the command because no command with the name [" + name + '] was found.';
this.message =
"Couldn't update the command because no command with the name [" +
name +
'] was found with ' +
JSON.stringify(typeInformation);
this.code = 400;
}
}
Expand Down Expand Up @@ -201,9 +219,10 @@ class DeviceGroupNotFound {
}
}
class GroupNotFound {
constructor(service, subservice) {
constructor(service, subservice, type) {
this.name = 'GROUP_NOT_FOUND';
this.message = 'Group not found for service [' + service + '] and subservice [' + subservice + ']';
this.message =
'Group not found for service [' + service + '] subservice [' + subservice + '] and type [' + type + ']';
this.code = 404;
}
}
Expand All @@ -229,16 +248,22 @@ class BadAnswer {
}
}
class BadTimestamp {
constructor(payload, entityName) {
constructor(payload, entityName, typeInformation) {
this.name = 'BAD_TIMESTAMP';
this.message = 'Invalid ISO8601 timestamp [' + payload + '] in [' + entityName + ']';
this.message =
'Invalid ISO8601 timestamp [' +
payload +
'] in [' +
entityName +
'] with: ' +
JSON.stringify(typeInformation);
this.code = 400;
}
}
class BadGeocoordinates {
constructor(payload) {
constructor(payload, device) {
this.name = 'BAD_GEOCOORDINATES';
this.message = 'Invalid rfc7946 coordinates [' + payload + ']';
this.message = 'Invalid rfc7946 coordinates [' + payload + '] in ' + JSON.stringify(device);
this.code = 400;
}
}
Expand Down
14 changes: 12 additions & 2 deletions lib/services/commands/commandRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ function updateCommand(service, subservice, deviceId, command, callback) {

callback(null, foundCommand);
} else {
callback(new errors.CommandNotFound(command.name));
const deviceInfo = {
service,
subservice,
deviceId
};
callback(new errors.CommandNotFound(command.name, deviceInfo));
}
}

Expand Down Expand Up @@ -144,7 +149,12 @@ function remove(service, subservice, deviceId, name, callback) {
delete registeredCommands[foundCommand._id];
callback(null, foundCommand);
} else {
callback(new errors.CommandNotFound(name));
const deviceInfo = {
service,
subservice,
deviceId
};
callback(new errors.CommandNotFound(name, deviceInfo));
}
}

Expand Down
41 changes: 26 additions & 15 deletions lib/services/commands/commandRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function findCommand(service, subservice, deviceId, name, callback) {
name
};

logger.debug(context, 'Looking for command [%s] for device [%s]', name, deviceId);
logger.debug(context, 'Looking for command [%s] for device [%s] with [%j]', name, deviceId, queryObj);

const query = Command.model.findOne(queryObj);

Expand All @@ -53,8 +53,14 @@ function findCommand(service, subservice, deviceId, name, callback) {
} else if (data) {
callback(null, data);
} else {
logger.debug(context, 'Command for DeviceID [%j] with name [%j] not found', deviceId, name);
callback(new errors.CommandNotFound(name));
logger.debug(
context,
'Command for DeviceID [%j] with name [%j] not found with [%j]',
deviceId,
name,
queryObj
);
callback(new errors.CommandNotFound(name, queryObj));
}
});
}
Expand Down Expand Up @@ -124,15 +130,15 @@ function listCommands(service, subservice, deviceId, callback) {

const query = Command.model.find(condition).sort();

async.series([query.exec.bind(query), Command.model.countDocuments.bind(Command.model, condition)], function (
error,
results
) {
callback(error, {
count: results[1],
commands: results[0].map(toObjectFn)
});
});
async.series(
[query.exec.bind(query), Command.model.countDocuments.bind(Command.model, condition)],
function (error, results) {
callback(error, {
count: results[1],
commands: results[0].map(toObjectFn)
});
}
);
}

function remove(service, subservice, deviceId, name, callback) {
Expand All @@ -159,9 +165,14 @@ function remove(service, subservice, deviceId, name, callback) {

callback(null, commandResult);
} else {
logger.debug(context, 'Command [%s] not found for removal.', name);

callback(new errors.CommandNotFound(name));
const deviceInfo = {
service,
subservice,
deviceId,
name
};
logger.debug(context, 'Command [%s] not found for removal with %j', name, deviceInfo);
callback(new errors.CommandNotFound(name, deviceInfo));
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/devices/deviceRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function storeDevice(newDevice, callback) {
}

if (registeredDevices[newDevice.service][newDevice.id]) {
callback(new errors.DuplicateDeviceId(newDevice.id));
callback(new errors.DuplicateDeviceId(newDevice));
} else {
registeredDevices[newDevice.service][newDevice.id] = deepClone(newDevice);
registeredDevices[newDevice.service][newDevice.id].creationDate = Date.now();
Expand Down
8 changes: 4 additions & 4 deletions lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function storeDevice(newDevice, callback) {
if (error.code === 11000) {
logger.debug(context, 'Tried to insert a device with duplicate ID in the database: %s', error);

callback(new errors.DuplicateDeviceId(newDevice.id));
callback(new errors.DuplicateDeviceId(newDevice));
} else {
logger.debug(context, 'Error storing device information: %s', error);

Expand Down Expand Up @@ -206,7 +206,7 @@ function findOneInMongoDB(queryParams, id, callback) {
} else {
logger.debug(context, 'Device [%s] not found.', id);

callback(new errors.DeviceNotFound(id));
callback(new errors.DeviceNotFound(id, queryParams));
}
});
}
Expand Down Expand Up @@ -282,7 +282,7 @@ function getByNameAndType(name, type, service, servicepath, callback) {
} else {
logger.debug(context, 'Device [%s] not found.', name);

callback(new errors.DeviceNotFound(name));
callback(new errors.DeviceNotFound(name, optionsQuery));
}
});
}
Expand Down Expand Up @@ -373,7 +373,7 @@ function getDevicesByAttribute(name, value, service, subservice, callback) {
} else {
logger.debug(context, 'Device [%s] not found.', name);

callback(new errors.DeviceNotFound(name));
callback(new errors.DeviceNotFound(name, filter));
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function registerDevice(deviceObj, callback) {
/* eslint-disable-next-line no-unused-vars */
function (error, device) {
if (!error) {
innerCb(new errors.DuplicateDeviceId(deviceObj.id));
innerCb(new errors.DuplicateDeviceId(deviceObj));
} else {
innerCb();
}
Expand Down Expand Up @@ -646,7 +646,7 @@ function findOrCreate(deviceId, apikey, group, callback) {
newDevice,
group
);
callback(new errors.DeviceNotFound(deviceId));
callback(new errors.DeviceNotFound(deviceId, newDevice));
}
} else {
callback(error);
Expand All @@ -671,7 +671,7 @@ function retrieveDevice(deviceId, apiKey, callback) {
} else {
logger.error(context, "Couldn't find device data for APIKey [%s] and DeviceId[%s]", deviceId, apiKey);

callback(new errors.DeviceNotFound(deviceId));
callback(new errors.DeviceNotFound(deviceId, { apikey: apiKey }));
}
});
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/services/devices/devices-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function updateEntityHandlerNgsiLD(deviceData, updatedDevice, callback) {
body
);

const errorObj = new errors.EntityGenericError(deviceData.id, deviceData.type, body);
const errorObj = new errors.EntityGenericError(deviceData.id, deviceData.type, deviceData, body);

callback(errorObj);
}
Expand Down Expand Up @@ -184,7 +184,7 @@ function updateEntityNgsiLD(deviceData, updatedDevice, callback) {
*/
function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
if (!deviceObj.id || !deviceObj.type) {
callback(new errors.MissingAttributes('Id or device missing'));
callback(new errors.MissingAttributes('Id or device missing', deviceObj));
return;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {

callback(null, oldDevice);
} else {
callback(new errors.DeviceNotFound(newDevice.id));
callback(new errors.DeviceNotFound(newDevice.id, newDevice));
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/services/devices/devices-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ function updateEntityHandlerNgsi2(deviceData, updatedDevice, callback) {
body
);

const errorObj = new errors.EntityGenericError(deviceData.id, deviceData.type, body, response.statusCode);
const errorObj = new errors.EntityGenericError(
deviceData.id,
deviceData.type,
deviceData,
body,
response.statusCode
);

callback(errorObj);
}
Expand Down Expand Up @@ -204,7 +210,7 @@ function updateEntityNgsi2(deviceData, updatedDevice, callback) {
// Format any GeoJSON attrs properly
options.json[att] = NGSIv2.formatGeoAttrs(options.json[att]);
} catch (error) {
return callback(new errors.BadGeocoordinates(JSON.stringify(options.json)));
return callback(new errors.BadGeocoordinates(JSON.stringify(options.json), deviceData));
}
}

Expand Down Expand Up @@ -243,7 +249,7 @@ function updateEntityNgsi2(deviceData, updatedDevice, callback) {
*/
function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
if (!deviceObj.id || !deviceObj.type) {
callback(new errors.MissingAttributes('Id or device missing'));
callback(new errors.MissingAttributes('Id or device missing', deviceObj));
return;
}

Expand Down Expand Up @@ -282,7 +288,7 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {

callback(null, oldDevice);
} else {
callback(new errors.DeviceNotFound(newDevice.id));
callback(new errors.DeviceNotFound(newDevice.id, newDevice));
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/services/groups/groupRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function exists(group) {

function createGroup(group, callback) {
if (exists(group)) {
callback(new errors.DuplicateGroup(group.resource, group.apikey));
callback(new errors.DuplicateGroup(group));
} else {
const storeGroup = _.clone(group);

Expand Down
2 changes: 1 addition & 1 deletion lib/services/groups/groupRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function createGroup(group, callback) {
group.apikey
);

callback(new errors.DuplicateGroup(group.resource, group.apikey));
callback(new errors.DuplicateGroup(group));
} else {
logger.debug(context, 'Error storing device group information: %s', error);

Expand Down
Loading

0 comments on commit 46fbcb2

Please sign in to comment.