diff options
| author | Stormwind <stormwind@stormwinds-page.de> | 2023-05-09 11:03:34 +0200 | 
|---|---|---|
| committer | Stormwind <stormwind@stormwinds-page.de> | 2023-05-09 12:37:15 +0200 | 
| commit | b582ca5d8aafdf9bfaccbe67c74aa532cbfc4f1a (patch) | |
| tree | fa5bd206af0191e5a002a6498d83bb50c8b61890 /lib | |
| parent | 8cb48215bfdc7352ea39ee2c7241bd4d87636198 (diff) | |
Add error handeling pages (primarily for 404)
Now you can add special error pages for http error codes.
Currently it works only for 404 errors. If a route is called, which
isn't defined, the framwork gave our a generic 404 error page.
Now you can redirect this error to a controller, which allowes you to
create a special error handling for that case yourself.
you just need to add a route "404 => MyErrorController" to your routing
list. If it's not defined, noting will change and the old error message
will be returned as beforehand.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/zero/router.rb | 24 | 
1 files changed, 23 insertions, 1 deletions
| diff --git a/lib/zero/router.rb b/lib/zero/router.rb index 7f16933..0d3859a 100644 --- a/lib/zero/router.rb +++ b/lib/zero/router.rb @@ -16,6 +16,14 @@ module Zero    #   router = Zero::Router.new(    #     '/foo/:id' => FooController    #   ) +  # +  #  # @example of a simple router +  #  #  which supports an extra controller for a 404 error page +  #   router = Zero::Router.new( +  #     '/' => WelcomeController, +  #     404 => NotFoundController +  #   ) +  #    class Router      # match for variables in routes      VARIABLE_MATCH = %r{:(\w+)[^/]?} @@ -41,7 +49,14 @@ module Zero      # @param routes [Hash] a map of URLs to rack compatible applications      def initialize(routes)        @routes = {} -      routes.to_a.sort_by(&:first).reverse.each do |route, target| + +      routes.to_a.sort_by{|x| x[0].to_s}.reverse.each do |route, target| +        # Support for special error handling +        if route.is_a? Integer +          @routes[route] = target +          next +        end +          @routes[            Regexp.new(REGEX_BEGINNING +                       route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) + @@ -58,6 +73,9 @@ module Zero      def call(env)        env[ENV_KEY_CUSTOM] ||= {}        @routes.each do |route, target| +        # Skip all routes, which keys are Integers +        next if route.is_a? Integer +          match = route.match(env[ENV_KEY_PATH_INFO])          if match            match.names.each_index do |i| @@ -66,6 +84,10 @@ module Zero            return target.call(env)          end        end +      # If no route is found, we call the target 404 error route, if it's set +      return @routes[404].call(env) unless @routes[404].nil? + +      # If no target for the 404 error is set, we return a default message        [404, {'Content-Type' => 'text/html'}, ['Not found!']]      end    end | 
