Skip to content

Commit

Permalink
feat(viwiWebSocket): report event name on errors on the WebSocket cha…
Browse files Browse the repository at this point in the history
…nnel
  • Loading branch information
wzr1337 committed Mar 12, 2017
1 parent 76fc722 commit 6421064
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var run = (options?:runOptions):Promise<void> => {
msg = JSON.parse(message);
}
catch(err) {
_viwiWebSocket.sendError(400, new Error(err));
_viwiWebSocket.sendError(msg.event, 400, new Error(err));
return;
}
let event = splitEvent(msg.event);
Expand All @@ -106,7 +106,7 @@ var run = (options?:runOptions):Promise<void> => {
wsMapping[basePath].handleWebSocketMessages(msg, _viwiWebSocket);
}
else {
_viwiWebSocket.sendError(403, new Error("Not Found"));
_viwiWebSocket.sendError(msg.event, 403, new Error("Not Found"));
}
});
});
Expand Down Expand Up @@ -149,7 +149,7 @@ class wsHandler {
this._subscriptions[_viwiWebSocket.id] = this._subscriptions[_viwiWebSocket.id] || {}; // init if not yet initialized

if (!eventObj.service || !eventObj.resource) {
_viwiWebSocket.sendError(400, new Error("event url malformed"));
_viwiWebSocket.sendError(msg.event, 400, new Error("event url malformed"));
return;
}

Expand All @@ -168,26 +168,26 @@ class wsHandler {
if (! _viwiWebSocket.sendData(msg.event, data.data)) element.complete();
},
(err:any) => {
if (! _viwiWebSocket.sendError(500, new Error(err))) element.complete();
if (! _viwiWebSocket.sendError(msg.event, 500, new Error(err))) element.complete();
});
}
else {
if (! _viwiWebSocket.sendError(404, new Error("Not Found"))) element.complete();
if (! _viwiWebSocket.sendError(msg.event, 404, new Error("Not Found"))) element.complete();
}
}
else if (eventObj.element && !this.resource.elementSubscribable)
{
_viwiWebSocket.sendError(503, new Error("Not Implemented"));
_viwiWebSocket.sendError(msg.event, 503, new Error("Not Implemented"));
}
if (!eventObj.element && this.resource.resourceSubscribable) {
// resource subscription
logger.info("New resource level subscription:", msg.event);
_viwiWebSocket.acknowledgeSubscription(msg.event);

this._subscriptions[_viwiWebSocket.id][msg.event] = this.resource.change
.subscribe((data:ResourceUpdate) => {
.subscribe((change:ResourceUpdate) => {
//@TODO: needs rate limit by comparing last update timestamp with last update
logger.info("New resource data:", data);
logger.info("New resource data:", change);
let elements = this.resource.getResource(/*parseNumberOrId(req.query.$offset), parseNumberOrId(req.query.$limit)*/);
if(elements) {
let resp = elements.map((value:BehaviorSubject<Element>) => {
Expand All @@ -196,16 +196,16 @@ class wsHandler {
if(! _viwiWebSocket.sendData(msg.event, resp)) this.resource.change.complete();
}
else {;
if(! _viwiWebSocket.sendError(404, new Error("Not found"))) this.resource.change.complete();
if(! _viwiWebSocket.sendError(msg.event, 404, new Error("Not found"))) this.resource.change.complete();
}
},
(err:any) => {
if(! _viwiWebSocket.sendError(500, new Error(err))) this.resource.change.complete();
if(! _viwiWebSocket.sendError(msg.event, 500, new Error(err))) this.resource.change.complete();
});
}
else if (!eventObj.element && !this.resource.resourceSubscribable)
{
_viwiWebSocket.sendError(503, new Error("Not Implemented"));
_viwiWebSocket.sendError(msg.event, 501, new Error("Not Implemented"));
}
break;

Expand All @@ -217,7 +217,7 @@ class wsHandler {
case "reauthorize":
default:
logger.error("Unsupported command on ws://:", msg.event);
_viwiWebSocket.sendError(501,new Error("Not Implemented"));
_viwiWebSocket.sendError(msg.event, 501, new Error("Not Implemented"));
break;
}

Expand Down
6 changes: 3 additions & 3 deletions src/viwiWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class viwiWebSocket {
*
* @returns true on successful send
*/
sendError(code:number, err:Error):boolean {
this._logger.debug(this.constructor.name + ".sendError():", event);
return this._send({type: "error", code: code, data: err.message});
sendError(event: string, code:number, err:Error):boolean {
this._logger.debug(this.constructor.name + ".sendError():", event, code, err.message);
return this._send({type: "error", "event": event, code: code, data: err.message});
}

/**
Expand Down

0 comments on commit 6421064

Please sign in to comment.