Skip to content

Commit

Permalink
fix(dao): handle .fromData when data is undefined (JS-45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Oct 24, 2019
1 parent 6e5e66d commit efe6858
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/dao/AlarmDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export class AlarmDAO extends AbstractDAO<number, OnmsAlarm> {
public async get(id: number): Promise<OnmsAlarm> {
return this.getOptions().then((opts) => {
return this.http.get(this.pathToAlarmsEndpoint() + '/' + id, opts.build()).then((result) => {
return this.fromData(result.data);
const alarm = this.fromData(result.data);
if (!alarm) {
throw new OnmsError(`AlarmDAO.get id={id} ReST request succeeded, but did not return a valid alarm.`);
}
return alarm;
});
});
}
Expand All @@ -69,9 +73,16 @@ export class AlarmDAO extends AbstractDAO<number, OnmsAlarm> {
}
throw new OnmsError('Expected an array of alarms but got "' + (typeof data) + '" instead.');
}
return data.map((alarmData) => {
const alarms = data.map((alarmData) => {
return this.fromData(alarmData);
});
// ugh, this cast is necessary to make tsc know there's nothing but OnmsAlarm objects
const ret = alarms.filter((alarm: OnmsAlarm | undefined) => alarm !== undefined) as OnmsAlarm[];
const diff = alarms.length - ret.length;
if (diff > 0) {
log.warn(`AlarmDAO.find ReST request succeeded, but {diff} alarms could not be parsed.`);
}
return ret;
});
});
}
Expand Down Expand Up @@ -299,6 +310,10 @@ export class AlarmDAO extends AbstractDAO<number, OnmsAlarm> {
public fromData(data: any) {
const alarm = new OnmsAlarm();

if (!data) {
return undefined;
}

alarm.id = this.toNumber(data.id);
alarm.count = data.count;
alarm.ackUser = data.ackUser;
Expand Down
23 changes: 20 additions & 3 deletions src/dao/EventDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {Util} from '../internal/Util';
import {OnmsEvent} from '../model/OnmsEvent';
import {OnmsParm} from '../model/OnmsParm';
import {OnmsServiceType} from '../model/OnmsServiceType';
import {Severities, OnmsSeverity} from '../model/OnmsSeverity';
import {OnmsSeverity} from '../model/OnmsSeverity';

import {log} from '../api/Log';

/**
* Data access for [[OnmsEvent]] objects.
Expand All @@ -25,7 +27,11 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
public async get(id: number): Promise<OnmsEvent> {
return this.getOptions().then((builder) => {
return this.http.get(this.pathToEventsEndpoint() + '/' + id, builder.build()).then((result) => {
return this.fromData(result.data);
const ev = this.fromData(result.data);
if (!ev) {
throw new OnmsError(`EventDAO.get id={id} ReST request succeeded, but did not return a valid event.`);
}
return ev;
});
});
}
Expand All @@ -49,9 +55,16 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
throw new OnmsError('Expected an array of events but got "' + (typeof data) + '" instead.');
}
}
return data.map((eventData: any) => {
const events = data.map((eventData: any) => {
return this.fromData(eventData);
});
// ugh, this cast is necessary to make tsc know there's nothing but OnmsEvent objects
const ret = events.filter((event: OnmsEvent | undefined) => event !== undefined) as OnmsEvent[];
const diff = events.length - ret.length;
if (diff > 0) {
log.warn(`EventDAO.find ReST request succeeded, but {diff} events could not be parsed.`);
}
return ret;
});
});
}
Expand All @@ -63,6 +76,10 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
public fromData(data: any) {
const event = new OnmsEvent();

if (!data) {
return undefined;
}

event.id = this.toNumber(data.id);
event.uei = data.uei;
event.label = data.label;
Expand Down
10 changes: 9 additions & 1 deletion src/dao/NodeDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
return this.http.get(this.pathToNodesEndpoint() + '/' + id, builder.build()).then((result) => {
const node = this.fromData(result.data);

if (!node) {
throw new OnmsError(`NodeDAO.get id={id} ReST request succeeded, but did not return a valid node.`);
}

if (recurse) {
return this.fetch(node);
} else {
Expand Down Expand Up @@ -210,9 +214,13 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
* Create a node object from a JSON object.
* @hidden
*/
public fromData(data: any): OnmsNode {
public fromData(data: any) {
const node = new OnmsNode();

if (!data) {
return undefined;
}

node.id = this.toNumber(data.id);
node.label = data.label;
node.location = data.location;
Expand Down

0 comments on commit efe6858

Please sign in to comment.