deliveroo/routemaster-drain

View on GitHub
lib/routemaster/errors.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Routemaster
  module Errors
    class BaseError < RuntimeError
      attr_reader :env

      def initialize(env)
        @env = env
        super(message)
      end

      def errors
        body.fetch('errors', {})
      end

      def message
        raise NotImplementedError
      end

      def body
        @body ||= deserialized_body
      end

      private

      def deserialized_body
        @env.body.empty? ? {} : JSON.parse(@env.body)
      end
    end

    class UnauthorizedResourceAccess < BaseError
      def message
        "Unauthorized Resource Access Error"
      end
    end

    class InvalidResource < BaseError
      def message
        "Invalid Resource Error"
      end
    end

    class ResourceNotFound < BaseError
      def message
        "Resource Not Found Error"
      end
    end

    class ResourceGone < BaseError
      def message
        "Resource Gone: #{env.url}"
      end
    end

    class FatalResource < BaseError
      def message
        "Fatal Resource Error. body: #{env.body}, url: #{env.url}, method: #{env.method}"
      end
    end

    class ConflictResource < BaseError
      def message
        "ConflictResourceError Resource Error"
      end
    end

    class IncompatibleVersion < BaseError
      def message
        headers = env.request_headers.select { |k, _| k != 'Authorization' }
        "Incompatible Version Error. headers: #{headers}, url: #{env.url}, method: #{env.method}"
      end
    end

    class ResourceThrottling < BaseError
      def message
        "Resource Throttling Error"
      end
    end

    class MethodNotAllowed < BaseError
      def message
        "Method Not Allowed"
      end
    end

    class BadGateway < BaseError
      def message
        "Bad Gateway"
      end
    end

    class ServiceNotAvailable < BaseError
      def message
        "Service Not Available"
      end
    end
  end
end