ruby-grape / ruby-grape/grape-swagger

Custom json format javascript error

Open
#198 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

swagger-1.2
Dominant language
Ruby
Stars
1.1k
Forks
479
Avg merge
2d 14h
Merged PRs (30d)
6

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with APIHelpers::JSendSuccessFormatter and the API class's formatter configuration, then trace the response through buildFrom1_1Spec in swagger-ef0b13e38d9e65fe2645c7219034874d.js. Done means the shown custom JSON response no longer causes the response.apis.length error, with coverage for the reported payload.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.