Rails Raise Custom Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of rails raise exception with message this site About Us Learn more about Stack Overflow the company Business
Rails Where To Put Custom Exceptions
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask ruby rescue custom exception Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign rails standard error up How to include Custom exception in Rails? up vote 4 down vote favorite I don't understand well how Rails include (or not?) some file from the app directory. For example, I've created a new directory app/exceptions for create my own exceptions. Now, from a helpers file, I want to raise one of my exception. Am I suppose to include something in this
Rails Exceptions
helper? The Helper: helpers/communications_helper.rb //should I include something or it's suppose to be autoloaded? module CommunicationsHelper begin. . . . raise ParamsException, "My exception is lauch!" rescue StandardError => e ... end end The exception: exceptions/params_exception.rb class ParamsException < StandardError def initialize(object, operation) puts "Dans paramsException" end end Nothing specific from my raise in the output... Thanks! EDIT: Thanks to all, your two answers was helpful in different way. I didn't raise well the exception like you said, but I've also forggot to update my config.rb. so now I 've: rescue StandardError => e raise ParamsError.new("truc", "truc") Other question, do you know where can I catch the raise? Cause I'm already in a catch block, so I'm little lost... ruby-on-rails ruby share|improve this question edited Dec 22 '14 at 14:43 asked Dec 20 '14 at 14:26 Julien Leray 791627 I would suggest to create a separate controller, and route errors to it. It would look simple. –Nithin Dec 20 '14 at 14:29 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted First, I think that you're raising your excepti
and rescuing custom errors in a Rails application. It's often useful to map custom Ruby errors to HTTP response rails raise error in model status codes and have Rails render the appropriate HTML error pages. For rails error types example, you might have a controller that is acting as a simple proxy to a third party service
Ruby Standard Error
such as Twitter or Facebook, and you need any of the HTTP errors encountered when calling those sites to be handled natively by your app. Another use case would http://stackoverflow.com/questions/27581130/how-to-include-custom-exception-in-rails be in a Service-oriented architecture (SOA), where you want any errors in your back end services propagated to your front end web application. In this post we'll demonstrate rescuing status errors in an imaginary proxy controller using the awesome Faraday gem. For the sake of brevity we've omitted the inclusion of tests though in the wild we'd build such https://wearestac.com/blog/raising-and-rescuing-custom-errors-in-rails a feature using TDD and our favourite test weapon, RSpec. Not Found To start, let's handle basic 404 Not Found errors that occur when calling a service. For this we'll need a custom error class that extends StandardError. # lib/errors/not_found.rb module Errors class NotFound < StandardError; end end Faraday provides a neat Rack-esque middleware feature. By creating our own custom middleware we can catch any Faraday 404s and raise our custom error. Furthermore, we can re-use the middleware anytime we need the same behaviour. # lib/errors/raise_error.rb module Errors class RaiseError < Faraday::Response::Middleware def on_complete(env) raise Errors::NotFound if env[:status] == 404 end end end Now for the proxy controller. # app/controllers/proxy_controller.rb class ProxyController < ApplicationController def index connection = Faraday.new(:url => 'http://someservice') do |f| f.adapter Faraday.default_adapter f.use Errors::RaiseError # Include custom middleware end response = connection.get('/some/resource') render :text => response.body end end At this point any NotFounds raised will still result in a 500 Internal Server Error in Rails. To alleviate this let's create a module that uses rescue_from, catches any custom NotFounds and renders the defau
deals with exceptions is strikingly similar to other languages (such as Java etc.). But, I think all the stuff http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/ I've been writing about Ruby lately has taken on a life of it's own for me :), so I am going to quickly go over this topic if only for completeness sakes. Raising Exceptions Getting Ruby to raise an exception is easy :). Ruby, like many other languages has a hierarchy of exception classes (that rails raise all inherit from the class Exception), some of these are familiar and fairly easy to produce, for example ZeroDivisionError or NoMethodError. Here is some code which will do just that: 1/0 or blah = Object.new blah.hello Of course you don't have to wait for Ruby to raise exceptions for you, you can do so explicitly rails raise custom in your code with the raise keyword (it is actually a method). Lets write a method where we explicitly raise an exception if the argument we pass in is false: def i_must_have_truth(value) raise TypeError, 'You must give me truth' if value == false end i_must_have_truth false this prints: D:\ruby-projects\scrap\lib\scrap.rb:15:in `i_must_have_truth': You must give me truth (TypeError) from D:\ruby-projects\scrap\lib\scrap.rb:18 As you can see we are able to raise a specific exception and pass it a message which then gets printed out to the console. There is also a handy shortcut, if you use raise without giving it a specific exceptions (or even without giving it an error message to display), Ruby will automatically raise a RuntimeException for you which is pretty handy: def i_must_have_truth(value) raise "Hello" end i_must_have_truth false this prints: D:\ruby-projects\scrap\lib\scrap.rb:15:in `i_must_have_truth': Hello (RuntimeError) from D:\ruby-projects\scrap\lib\scrap.rb:18 Rescuing Exceptions So far so good, but life would be pretty tough if we couldn't handle the exceptions that get thrown in any way, This is where