27
Dec

custom exceptions in merb

在开发应用过程中难免需要根据实际情况自定义一些异常处理机制,这方面Merb已经将底部的架子为我们准备好了,我们只需要简单的在此基础上进行扩展就可以搞的很舒服。
Merb默认生成的Application会提供一个Exception的Controller,并包含了两个样版异常NotFound和NotAcceptable。现在我们添加一个自定义的异常在这里,假定名称为NotExists,并为其创建对应的View。
在Action中调用
def show
product = Product.find(params[:id])
raise NotExists unless product
[...]
end

这时会报错,称并不存在NotExists常量,Google了一下并没有找到合理的解释,直接看了Merb的源代码才发现,自定义异常不是这样的用法。

在Merb里如果要自定义异常,需要将自定义异常继承自一个Merb自身已存的异常,比如这样
class NotExists < Merb::ControllerExceptions::NotFound ; end
并且需要在使用该异常的Controller里进行声明(放在Application里自然会是一个不错的选择),再跑一下上面报错的程序应该就可以得到正确的结果啦~ BTW:为了这个我可查了好一会资料 :)

以下是Merb对标准Http异常的封装
class Informational < Merb::ControllerExceptions::Base; end
class Continue < Merb::ControllerExceptions::Informational; self.status = 100; end
class SwitchingProtocols < Merb::ControllerExceptions::Informational; self.status = 101; end
class Successful < Merb::ControllerExceptions::Base; end
class OK < Merb::ControllerExceptions::Successful; self.status = 200; end
class Created < Merb::ControllerExceptions::Successful; self.status = 201; end
class Accepted < Merb::ControllerExceptions::Successful; self.status = 202; end
class NonAuthoritativeInformation < Merb::ControllerExceptions::Successful; self.status = 203; end
class NoContent < Merb::ControllerExceptions::Successful; self.status = 204; end
class ResetContent < Merb::ControllerExceptions::Successful; self.status = 205; end
class PartialContent < Merb::ControllerExceptions::Successful; self.status = 206; end
class Redirection < Merb::ControllerExceptions::Base; end
class MultipleChoices < Merb::ControllerExceptions::Redirection; self.status = 300; end
class MovedPermanently < Merb::ControllerExceptions::Redirection; self.status = 301; end
class MovedTemporarily < Merb::ControllerExceptions::Redirection; self.status = 302; end
class SeeOther < Merb::ControllerExceptions::Redirection; self.status = 303; end
class NotModified < Merb::ControllerExceptions::Redirection; self.status = 304; end
class UseProxy < Merb::ControllerExceptions::Redirection; self.status = 305; end
class TemporaryRedirect < Merb::ControllerExceptions::Redirection; self.status = 307; end
class ClientError < Merb::ControllerExceptions::Base; end
class BadRequest < Merb::ControllerExceptions::ClientError; self.status = 400; end
class MultiPartParseError < Merb::ControllerExceptions::BadRequest; end
class Unauthorized < Merb::ControllerExceptions::ClientError; self.status = 401; end
class PaymentRequired < Merb::ControllerExceptions::ClientError; self.status = 402; end
class Forbidden < Merb::ControllerExceptions::ClientError; self.status = 403; end
class NotFound < Merb::ControllerExceptions::ClientError; self.status = 404; end
class ActionNotFound < Merb::ControllerExceptions::NotFound; end
class TemplateNotFound < Merb::ControllerExceptions::NotFound; end
class LayoutNotFound < Merb::ControllerExceptions::NotFound; end
class MethodNotAllowed < Merb::ControllerExceptions::ClientError; self.status = 405; end
class NotAcceptable < Merb::ControllerExceptions::ClientError; self.status = 406; end
class ProxyAuthenticationRequired < Merb::ControllerExceptions::ClientError; self.status = 407; end
class RequestTimeout < Merb::ControllerExceptions::ClientError; self.status = 408; end
class Conflict < Merb::ControllerExceptions::ClientError; self.status = 409; end
class Gone < Merb::ControllerExceptions::ClientError; self.status = 410; end
class LengthRequired < Merb::ControllerExceptions::ClientError; self.status = 411; end
class PreconditionFailed < Merb::ControllerExceptions::ClientError; self.status = 412; end
class RequestEntityTooLarge < Merb::ControllerExceptions::ClientError; self.status = 413; end
class RequestURITooLarge < Merb::ControllerExceptions::ClientError; self.status = 414; end
class UnsupportedMediaType < Merb::ControllerExceptions::ClientError; self.status = 415; end
class RequestRangeNotSatisfiable < Merb::ControllerExceptions::ClientError; self.status = 416; end
class ExpectationFailed < Merb::ControllerExceptions::ClientError; self.status = 417; end
class ServerError < Merb::ControllerExceptions::Base; end
class InternalServerError < Merb::ControllerExceptions::ServerError; self.status = 500; end
class NotImplemented < Merb::ControllerExceptions::ServerError; self.status = 501; end
class BadGateway < Merb::ControllerExceptions::ServerError; self.status = 502; end
class ServiceUnavailable < Merb::ControllerExceptions::ServerError; self.status = 503; end
class GatewayTimeout < Merb::ControllerExceptions::ServerError; self.status = 504; end
class HTTPVersionNotSupported < Merb::ControllerExceptions::ServerError; self.status = 505; end

No Comments

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (will not be published)(required)
Website

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.raecoo.com),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

    About

    Tag Cloud