| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,23 @@ | |||
| 1 | 1 | "use strict"; | |
| 2 | - function applyMiddleware(text, middleware, next) { | ||
| 3 | - middleware(text, next); | ||
| 2 | + function isErrorHandingMiddleware(middleware) { | ||
| 3 | + // 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); | ||
| 4 | 21 | } | |
| 5 | 22 | ||
| 6 | 23 | export default class Junction { | |
@@ -18,7 +35,7 @@ export default class Junction { | |||
| 18 | 35 | if (!middleware) { | |
| 19 | 36 | return callback(error, data); | |
| 20 | 37 | } | |
| 21 | - applyMiddleware(data, middleware, next); | ||
| 38 | + applyMiddleware(error, data, middleware, next); | ||
| 22 | 39 | }; | |
| 23 | 40 | next(null, text); | |
| 24 | 41 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + // LICENSE : MIT | ||
| 2 | + "use strict"; | ||
| 3 | + import assert from "power-assert"; | ||
| 4 | + import Junction from "../../src/connect/junction"; | ||
| 5 | + describe("junction", function () { | ||
| 6 | + describe("when", function () { | ||
| 7 | + it("should", function (done) { | ||
| 8 | + var junction = new Junction(); | ||
| 9 | + junction.use(function errorHandling(error, text, next) { | ||
| 10 | + next(error); | ||
| 11 | + }); | ||
| 12 | + junction.use(function toUpper(text, next) { | ||
| 13 | + next(null, text.toLocaleUpperCase()); | ||
| 14 | + }); | ||
| 15 | + junction.use(function addDesu(text, next) { | ||
| 16 | + next(null, text + " suffix"); | ||
| 17 | + }); | ||
| 18 | + junction.process("text", (error, result) => { | ||
| 19 | + if (error) { | ||
| 20 | + return done(error); | ||
| 21 | + } | ||
| 22 | + assert.equal(result, "TEXT suffix"); | ||
| 23 | + done(); | ||
| 24 | + }); | ||
| 25 | + }); | ||
| 26 | + }); | ||
| 27 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments