Revision 1443d37f
Added by Martin Bacovsky almost 12 years ago
- ID 1443d37f1669f467e0cc2ab6fe6cd04801224070
app/controllers/api/base_controller.rb | ||
---|---|---|
|
||
before_filter :set_default_response_format
|
||
|
||
def process_error hash = {}
|
||
hash[:object] ||= get_resource || raise("Param 'object' was not defined")
|
||
|
||
hash[:json_code] ||= :unprocessable_entity
|
||
|
||
errors = if hash[:object].respond_to?(:errors)
|
||
logger.info "Failed to save: #{hash[:object].errors.full_messages.join(", ")}"
|
||
hash[:object].errors.full_messages
|
||
else
|
||
raise("Object has to respond to errors")
|
||
end
|
||
|
||
render :json => {"errors" => errors} , :status => hash[:json_code]
|
||
end
|
||
|
||
def get_resource
|
||
instance_variable_get(:"@#{controller_name.singularize}")
|
||
end
|
||
|
||
|
||
def process_response condition, response = nil
|
||
if condition
|
||
response ||= get_resource
|
||
respond_with response
|
||
else
|
||
process_error
|
||
end
|
||
end
|
||
|
||
|
||
def request_from_katello_cli?
|
||
request.headers['User-Agent'].to_s =~ /^katello-cli/
|
||
end
|
||
|
||
protected
|
||
# searches for an object based on its name and assign it to an instance variable
|
||
# required for models which implement the to_param method
|
Also available in: Unified diff
Fixed error handling in BaseController