grape-swagger
grape-swagger copied to clipboard
Custom json format javascript error
Custmer json format add {"status": "202", "code": "2001","data": {grape object json}}, But i get a javascript error.
module API
module APIHelpers
module JSendSuccessFormatter
def self.call(object, env)
{ status: '200', code: '2001', data: object }.to_json
end
end
module JSendErrorFormatter
def self.call(message, backtrace, options, env)
if message.is_a?(Hash)
{ :status => 'fail', :data => message }.to_json
else
{ :status => 'error', :message => message }.to_json
end
end
end
module ExceptionHandlers
def self.included(base)
base.instance_eval do
rescue_from Grape::Exceptions::ValidationErrors do |e|
Rack::Response.new({
error: { code: 1001, message: e.message }
}.to_json, e.status)
end
end
end
end
end
end
module API
class API < Grape::API
version 'v1'
prefix 'api'
format :json
helpers APIHelpers
formatter :json, APIHelpers::JSendSuccessFormatter
error_formatter :json, APIHelpers::JSendErrorFormatter
......
.....
optput
{
"status": "200",
"code": "2001",
"data": {
"id": 1,
"title": "title",
"summary": "summary",
"content": "content",
"title_link": "title_link",
"must_read": true,
"slug": "slug",
"state": "publish",
"draft_key": "draft_key",
"created_at": "2015-01-13T14:24:05.943+08:00",
"updated_at": "2015-01-13T14:24:05.943+08:00",
"cover": {
"url": null
}
}
}
[Error] TypeError: undefined is not an object (evaluating 'response.apis.length')
buildFrom1_1Spec (swagger-ef0b13e38d9e65fe2645c7219034874d.js, line 217)
response (swagger-ef0b13e38d9e65fe2645c7219034874d.js, line 143)
response (swagger-ef0b13e38d9e65fe2645c7219034874d.js, line 1479)
emit (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1391)
emit (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1191)
(anonymous function) (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1211)
setBodyAndFinish (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1815)
(anonymous function) (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1832)
emit (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 1388)
handle (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 2715)
onreadystatechange (shred.bundle-945ecf92347d2bf3ab1b35dd24ed25cb.js, line 2584)
This looks like an error in swagger-ui, I would run the development version and find out what swagger-ui is unhappy with (and then we can fix it here).