Skip to content

Commit

Permalink
Add some error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Jan 30, 2020
1 parent a5195eb commit d52bb88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/controllers/apitome/docs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ def rendered_markdown(string)

def formatted_body(body, type)
return body if body == " "
return body if body == "[binary data]"
return body if body == "[binary data]" # see https://github.com/zipmark/rspec_api_documentation/blob/560c3bdc7bd5581e7c223334390221ecfc910be8/lib/rspec_api_documentation/client_base.rb#L88-L96
if type =~ /json/ && body.present?
JSON.pretty_generate(JSON.parse(body))
else
body
end
rescue => e
if Apitome.configuration.formatted_body_error_handler
Apitome.configuration.formatted_body_error_handler.call(e, body, type)
else
raise
end
end

def param_headers(params)
Expand Down
20 changes: 20 additions & 0 deletions app/views/apitome/docs/_example.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,42 @@
<div id="<%= "request-#{index}" %>">
<h3><%= t(:request, scope: :apitome) %></h3>
<div class="request">
<% begin %>
<%= render partial: 'apitome/docs/route', locals: {request: request, index: index} %>
<%= render partial: 'apitome/docs/headers', locals: {request: request, index: index, headers: request['request_headers']} %>
<%= render partial: 'apitome/docs/query', locals: {request: request, index: index} unless request['request_query_parameters'].empty? %>
<%= render partial: 'apitome/docs/body', locals: {request: request, index: index, body: request['request_body'], type: request['request_content_type']} if request['request_body'] %>
<%= render partial: 'apitome/docs/curl', locals: {request: request, index: index} if request['curl'] %>
<%
rescue => e
if Apitome.configuration.example_error_handler
Apitome.configuration.example_error_handler.call(e, "request", request)
else
raise
end
end
%>
</div>

<h3><%= t(:response, scope: :apitome) %></h3>
<div class="response">
<% begin %>
<%- if Apitome.configuration.simulated_response %>
<%= link_to('Simulated Response', simulated_path(example[:link])) if example[:link].present? %>
<%- end %>
<%= render partial: 'apitome/docs/response_fields', locals: {params: example['response_fields']} if example['response_fields'].size > 0 %>
<%= render partial: 'apitome/docs/status', locals: {request: request, index: index} %>
<%= render partial: 'apitome/docs/headers', locals: {request: request, index: index, headers: request['response_headers']} %>
<%= render partial: 'apitome/docs/body', locals: {request: request, index: index, body: request['response_body'], type: request['response_content_type']} if request['response_body'] %>
<%
rescue => e
if Apitome.configuration.example_error_handler
Apitome.configuration.example_error_handler.call(e, "response", request)
else
raise
end
end
%>
</div>
</div>
<% end %>
4 changes: 4 additions & 0 deletions lib/apitome/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Configuration
:http_basic_authentication,
:precompile_assets,
:simulated_response,
:formatted_body_error_handler,
:example_error_handler,
])

@@mount_at = "/api/docs"
Expand All @@ -39,6 +41,8 @@ class Configuration
@@http_basic_authentication = nil
@@precompile_assets = true
@@simulated_response = true
@@formatted_body_error_handler = nil
@@example_error_handler = nil

def self.root=(path)
@@root = Pathname.new(path.to_s) if path.present?
Expand Down

0 comments on commit d52bb88

Please sign in to comment.