Skip to content
Browse files

added catchall error handler

  • Loading branch information...
1 parent 266dae7 commit c52ea7649a84a8c2e3c443b75ef62c9168835124 @dvv dvv committed Mar 12, 2012
Showing with 48 additions and 2 deletions.
  1. +17 −2 lib/luvit/core.lua
  2. +31 −0 tests/test-error.lua
View
19 lib/luvit/core.lua
@@ -96,11 +96,26 @@ Also it can easily be sub-classed.
local Emitter = Object:extend()
core.Emitter = Emitter
--- By default, and error events that are not listened for should thow errors
+-- By default, and error events that are not listened for should throw errors
function Emitter:missingHandlerType(name, ...)
if name == "error" then
local args = {...}
- error(tostring(args[1]))
+ --error(tostring(args[1]))
+ -- we define catchall error handler
+ if self ~= process then
+ -- if process has an error handler
+ local handlers = rawget(process, "handlers")
+ if handlers and handlers["error"] then
+ -- delegate to process error handler
+ process:emit("error", ..., self)
+ else
+ debug("UNHANDLED ERROR", ...)
+ error("UNHANDLED ERROR. Define process:on('error', handler) to catch such errors")
+ end
+ else
+ debug("UNHANDLED ERROR", ...)
+ error("UNHANDLED ERROR. Define process:on('error', handler) to catch such errors")
+ end
end
end
View
31 tests/test-error.lua
@@ -0,0 +1,31 @@
+--[[
+
+Copyright 2012 The Luvit Authors. All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS-IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+--]]
+
+require("helper")
+
+local obj = require('core').Emitter:new()
+
+process:on("error", function (err, source)
+ assert(deep_equal(err, { a = "b" }))
+ assert(source == obj)
+ process.exit(0)
+end)
+
+obj:emit("error", { a = "b" })
+
+assert(false)

0 comments on commit c52ea76

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