Skip to content

Commit d8b43f7

Browse files
committed
Auth improvements
1 parent 5f1b765 commit d8b43f7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/socket/base.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const cds = require("@sap/cds");
55
const crypto = require("crypto");
66
const path = require("path");
7+
const { inspect } = require("util");
78

89
/**
910
* Base class for a websocket server
@@ -229,14 +230,15 @@ class SocketServer {
229230
* @returns {[function]} Returns a list of middleware functions
230231
*/
231232
middlewares() {
233+
const base = this;
232234
function wrapMiddleware(middleware) {
233235
return (socket, next) => {
234236
let nextCalled = false;
235237
const wrapNext = (err) => {
236238
delete socket.request._next;
237239
if (!nextCalled) {
238240
nextCalled = true;
239-
next(err);
241+
next(base.toError(err));
240242
}
241243
};
242244
socket.request._next = wrapNext;
@@ -526,6 +528,31 @@ class SocketServer {
526528
}
527529
}
528530
}
531+
532+
/**
533+
* Check if error is an instance of Error
534+
* @param err Error
535+
* @returns {boolean} True if error is an instance of Error
536+
*/
537+
isError(err) {
538+
try {
539+
return err instanceof Error || Object.prototype.toString.call(err) === "[object Error]";
540+
} catch {
541+
return false;
542+
}
543+
}
544+
545+
/**
546+
* Convert error to instance of Error
547+
* @param err Error
548+
* @returns {Error} Error instance
549+
*/
550+
toError(err) {
551+
if ([undefined, null].includes(err)) {
552+
return err;
553+
}
554+
return this.isError(err) ? err : new Error(inspect(err));
555+
}
529556
}
530557

531558
module.exports = SocketServer;

test/_env/approuter/default-env.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"url": "http://localhost:4004",
66
"forwardAuthToken": true
77
}
8-
]
8+
],
9+
"COOKIE_BACKWARD_COMPATIBILITY": true
910
}

0 commit comments

Comments
 (0)