Module: Mayu::Server::Errors

Extended by:
T::Sig
Defined in:
lib/mayu/server/errors.rb

Defined Under Namespace

Classes: CookieNotSet, FileNotFound, InvalidMethod, InvalidSecFetchHeader, InvalidToken, ServerError, ServerIsShuttingDown, SessionAlreadyResumed, SessionNotFound, UnauthorizedSessionCookie

Class Method Summary collapse

Class Method Details

.handle_exceptions(&block) ⇒ Object



36
37
38
# File 'lib/mayu/server/errors.rb', line 36

def self.handle_exceptions(&block)
  respond_to_exceptions { log_exceptions { yield } }
end

.log_exceptions(&block) ⇒ Object



45
46
47
48
49
50
# File 'lib/mayu/server/errors.rb', line 45

def self.log_exceptions(&block)
  yield
rescue => e
  Console.logger.error(self, "#{e.class.name}: #{e.message}")
  raise
end

.respond_to_exceptions(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mayu/server/errors.rb', line 57

def self.respond_to_exceptions(&block)
  yield
rescue Errno::ENOENT => e
  text_response(404, "file not found")
rescue FileNotFound => e
  text_response(404, e.message.to_s)
rescue CookieNotSet => e
  text_response(403, "session cookie not set")
rescue SessionNotFound => e
  text_response(404, "session not found")
rescue Mayu::EncryptedMarshal::DecryptError => e
  text_response(403, "decrypt error")
rescue Mayu::EncryptedMarshal::ExpiredError => e
  text_response(403, "session expired")
rescue InvalidToken => e
  text_response(403, "invalid token")
rescue SessionAlreadyResumed => e
  text_response(409, "already resumed")
rescue Session::AlreadyRunningError => e
  text_response(409, "already running")
rescue ServerIsShuttingDown => e
  # https://fly.io/docs/reference/fly-replay/#fly-replay
  text_response(
    405,
    "invalid method",
    { "fly-replay" => "elsewhere=true" }
  )
rescue InvalidMethod => e
  text_response(405, "invalid method")
rescue UnauthorizedSessionCookie => e
  text_response(403, "session cookie is invalid")
rescue InvalidSecFetchHeader => e
  text_response(415, e.message)
rescue StandardError
  text_response(500, "error")
end

.text_response(code, text, headers = {}) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/mayu/server/errors.rb', line 101

def self.text_response(code, text, headers = {})
  Protocol::HTTP::Response[
    code,
    { "content-type" => "text/plain", **headers },
    [text]
  ]
end