Unify ClientError and ServerError into ApiError

in Ruby generator
This commit is contained in:
xhh 2015-06-15 16:38:08 +08:00
parent e9c1dd7842
commit 9d5928551b
9 changed files with 67 additions and 20 deletions

View File

@ -73,6 +73,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("swagger.mustache", baseFolder, "swagger.rb"));
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "request.mustache", swaggerFolder, "request.rb"));
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "response.mustache", swaggerFolder, "response.rb"));
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "api_error.mustache", swaggerFolder, "api_error.rb"));
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "version.mustache", swaggerFolder, "version.rb"));
supportingFiles.add(new SupportingFile("swagger" + File.separatorChar + "configuration.mustache", swaggerFolder, "configuration.rb"));
supportingFiles.add(new SupportingFile("base_object.mustache", modelFolder, "base_object.rb"));

View File

@ -52,7 +52,7 @@ module {{moduleName}}
return if Swagger.authenticated?
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
raise ClientError, "Username and password are required to authenticate."
raise ApiError, "Username and password are required to authenticate."
end
request = Swagger::Request.new(
@ -69,10 +69,4 @@ module {{moduleName}}
end
end
end
class ServerError < StandardError
end
class ClientError < StandardError
end
end

View File

@ -0,0 +1,26 @@
module {{moduleName}}
module Swagger
class ApiError < StandardError
attr_reader :code, :response_headers, :response_body
# Usage examples:
# ApiError.new
# ApiError.new("message")
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
# ApiError.new(:code => 404, :message => "Not Found")
def initialize(arg = nil)
if arg.is_a? Hash
arg.each do |k, v|
if k.to_s == 'message'
super v
else
instance_variable_set "@#{k}", v
end
end
else
super arg
end
end
end
end
end

View File

@ -9,9 +9,11 @@ module {{moduleName}}
def initialize(raw)
self.raw = raw
case self.code
when 500..510 then raise(ServerError, self.body)
when 299..426 then raise(ClientError, self.body)
unless raw.success?
fail ApiError.new(:code => code,
:response_headers => headers,
:response_body => body),
raw.status_message
end
end

View File

@ -2,6 +2,7 @@
require '{{gemName}}/monkey'
require '{{gemName}}/swagger'
require '{{gemName}}/swagger/configuration'
require '{{gemName}}/swagger/api_error'
require '{{gemName}}/swagger/request'
require '{{gemName}}/swagger/response'
require '{{gemName}}/swagger/version'

View File

@ -2,6 +2,7 @@
require 'swagger_client/monkey'
require 'swagger_client/swagger'
require 'swagger_client/swagger/configuration'
require 'swagger_client/swagger/api_error'
require 'swagger_client/swagger/request'
require 'swagger_client/swagger/response'
require 'swagger_client/swagger/version'

View File

@ -52,7 +52,7 @@ module SwaggerClient
return if Swagger.authenticated?
if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
raise ClientError, "Username and password are required to authenticate."
raise ApiError, "Username and password are required to authenticate."
end
request = Swagger::Request.new(
@ -69,10 +69,4 @@ module SwaggerClient
end
end
end
class ServerError < StandardError
end
class ClientError < StandardError
end
end

View File

@ -0,0 +1,26 @@
module SwaggerClient
module Swagger
class ApiError < StandardError
attr_reader :code, :response_headers, :response_body
# Usage examples:
# ApiError.new
# ApiError.new("message")
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
# ApiError.new(:code => 404, :message => "Not Found")
def initialize(arg = nil)
if arg.is_a? Hash
arg.each do |k, v|
if k.to_s == 'message'
super v
else
instance_variable_set "@#{k}", v
end
end
else
super arg
end
end
end
end
end

View File

@ -9,9 +9,11 @@ module SwaggerClient
def initialize(raw)
self.raw = raw
case self.code
when 500..510 then raise(ServerError, self.body)
when 299..426 then raise(ClientError, self.body)
unless raw.success?
fail ApiError.new(:code => code,
:response_headers => headers,
:response_body => body),
raw.status_message
end
end