Skip to content
Browse files

Improved Error Handling

Added documentation and implementation for improved error handling. It
is now also possible to call the next function with an error object
instead of throwing it.
  • Loading branch information...
1 parent a2fc440 commit cd1c03d7077960b58f5839541c626343fd4d25ac @niclashoyer committed
Showing with 59 additions and 4 deletions.
  1. +19 −0 README.md
  2. +1 −1 package.json
  3. +11 −2 src/Onion.coffee
  4. +28 −1 test/Onion.coffee
View
19 README.md
@@ -118,6 +118,25 @@ onion.construct ->
return [skin1, skin2]
```
+
+#### Error Handling
+The error skin is a special middleware that is executed if a
+middleware throws an error. The error middleware can then decide
+how to handle the error. The error is given to the middleware after
+the given arguments, that means that the error middleware has the
+same signature as any other middleware, but with an error object
+appended. For example
+
+```coffeescript
+@error = (args..., next, error) ->
+ console.log error.message
+ next()
+```
+
+will print the error message to the console and call the next
+middleware. By calling `next` the error can be ignored, thus the next
+middleware will be called.
+
#### Skin Constructor
The construct method takes a function that returns the middleware (the so
called *skin constructor*). The given function is executed in the scope
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "onion",
- "version": "0.2.1-3",
+ "version": "0.2.2-1",
"description": "Simple and flexible middleware stack that enables you to add a middleware layer to just about anything",
"author": {
"name": "Niclas Hoyer",
View
13 src/Onion.coffee
@@ -1,5 +1,6 @@
###
Copyright (c) 2011 Niclas Hoyer
+Copyright (c) 2013 Fiona Schmidtke
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
@@ -70,7 +71,8 @@ class Onion
# next()
#
# will print the error message to the console and call the next
- # middleware.
+ # middleware. By calling `next` the error can be ignored, thus the next
+ # middleware is called.
#
# The default error handler will just throw the error again.
if not @error
@@ -286,7 +288,14 @@ class Onion
# With the help of the `next` function a middleware can pass control
# to the next middleware, if another middleware should give a value
# back.
- next = =>
+ next = (err) =>
+ # If there was an error given from the last middleware,
+ if err?
+ # we call the error skin directly and append the error.
+ @error.apply @, args.concat [next,err]
+ # No more skins will be peeled.
+ return
+
# We first take the next middleware from the stack.
skin = skins[index++]
View
29 test/Onion.coffee
@@ -299,5 +299,32 @@ test "double peeling", ->
# ensure that we got exactly one egg.
@equal eggs, 1, "middleware executed once"
+# # Error Handling #
+# Just test the error handling, in case there is an error with the errors.
+test "error handling", ->
+ onion = new Onion
-
+ # With start with zero eggs.
+ eggs = 0
+
+ # We stack a skin that throws an exception, the first egg.
+ onion.stack (next) ->
+ throw new Error 'One egg please!'
+ next()
+
+ # We stack a skin that returns an error to the next funtion, the second egg.
+ onion.stack (next) ->
+ next new Error 'Another egg please!'
+
+ # Our error handler counts the eggs.
+ onion.error = (next, err) ->
+ # Add one egg and
+ eggs++
+ # ignore the error.
+ next()
+
+ # We peel the onion and
+ onion.peel()
+
+ # ensure that there are exactly two eggs.
+ @equal eggs, 2, "two errors catched"

0 comments on commit cd1c03d

Please sign in to comment.
Something went wrong with that request. Please try again.