tests: "wc -c" portability for *BSDs
[rainbows.git] / lib / rainbows / error.rb
blob8917c4d764eea0f8c69df0622e511f90ccc4c126
1 # -*- encoding: binary -*-
2 # :enddoc:
3 module Rainbows::Error
5   # if we get any error, try to write something back to the client
6   # assuming we haven't closed the socket, but don't get hung up
7   # if the socket is already closed or broken.  We'll always ensure
8   # the socket is closed at the end of this function
9   def self.write(io, e)
10     msg = response(e) and Kgio.trywrite(io, msg)
11     rescue
12   end
14   def self.app(e)
15     Unicorn.log_error(Rainbows.server.logger, "app error", e)
16     rescue
17   end
19   def self.listen_loop(e)
20     Rainbows.alive or return
21     Unicorn.log_error(Rainbows.server.logger, "listen loop error", e)
22     rescue
23   end
25   def self.response(e)
26     case e
27     when EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EINVAL,
28          Errno::EBADF, Errno::ENOTCONN
29       # swallow error if client shuts down one end or disconnects
30     when Unicorn::HttpParserError
31       Rainbows::Const::ERROR_400_RESPONSE # try to tell the client they're bad
32     when IOError # HttpParserError is an IOError
33     else
34       app(e)
35       Rainbows::Const::ERROR_500_RESPONSE
36     end
37   end
38 end