We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4902598 commit 6d33c42Copy full SHA for 6d33c42
src/connect/junction.js
@@ -1,6 +1,23 @@
1
"use strict";
2
-function applyMiddleware(text, middleware, next) {
3
- middleware(text, next);
+function isErrorHandingMiddleware(middleware) {
+ // middleware(error, text, next)
4
+ var arity = middleware.length;
5
+ return arity === 3;
6
+}
7
+function applyMiddleware(error, text, middleware, next) {
8
+ let errorOnMiddleware = null;
9
+ try {
10
+ if (error && isErrorHandingMiddleware(middleware)) {
11
+ middleware(error, text, next);
12
+ } else {
13
+ middleware(text, next);
14
+ }
15
+ return;
16
+ } catch (error) {
17
+ errorOnMiddleware = error;
18
19
+ // skip the middleware or Error on the middleware
20
+ next(errorOnMiddleware, text);
21
}
22
23
export default class Junction {
@@ -18,7 +35,7 @@ export default class Junction {
35
if (!middleware) {
36
return callback(error, data);
37
- applyMiddleware(data, middleware, next);
38
+ applyMiddleware(error, data, middleware, next);
39
};
40
next(null, text);
24
41
test/connect/junction-test.js
@@ -0,0 +1,27 @@
+// LICENSE : MIT
+"use strict";
+import assert from "power-assert";
+import Junction from "../../src/connect/junction";
+describe("junction", function () {
+ describe("when", function () {
+ it("should", function (done) {
+ var junction = new Junction();
+ junction.use(function errorHandling(error, text, next) {
+ next(error);
+ });
+ junction.use(function toUpper(text, next) {
+ next(null, text.toLocaleUpperCase());
+ junction.use(function addDesu(text, next) {
+ next(null, text + " suffix");
+ junction.process("text", (error, result) => {
+ if (error) {
+ return done(error);
+ assert.equal(result, "TEXT suffix");
+ done();
25
26
27
+});
0 commit comments