- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让 Grape API 以 json 格式回答其所有动词。问题是我无法回答 json 格式的路由错误。我什至无法挽救 ActionController::RoutingError。
我已阅读此链接 https://github.com/intridea/grape/pull/342我已将级联指令设置为 false,但 API 仅以纯文本形式回答“未找到”。
$ curl -X GET http://localhost:3000/api/wrong_uri
Not Found
带有详细选项:
$ curl -X GET http://localhost:3000/api/wrong_uri -v
* About to connect() to localhost port 3000 (#0)
* Trying 127.0.0.1... connected
> GET /api/wrong_uri HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/html
< Cache-Control: no-cache
< X-Request-Id: 3cc74a78-3295-4c1f-b989-66d6fac50e5c
< X-Runtime: 0.002980
< Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
< Date: Tue, 06 May 2014 05:46:33 GMT
< Content-Length: 9
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
Not Found
我的葡萄文件/app/api/api.rb
module API
class Base < Grape::API
format :json
default_error_formatter :json
cascade false
rescue_from CanCan::AccessDenied do |e|
Rack::Response.new({ error: "Forbidden", detail: "Access denied", status: '403' }.to_json, 403).finish
end
# it doesn't catch ActionController::RoutingError
# rescue_from :all do |e|
# error_response({ message: "rescued from #{e.class.name}" })
# end
helpers ApiHelpers
mount API::Auth
mount API::V2
mount API::V1
end
end
最佳答案
通过指定 cascade false
你是说 Grape 应该被允许在不受 Rails 干扰的情况下处理 HTTP 错误。因此,当请求 API 挂载点下的不匹配 URI 时,不会生成 ActionController::RoutingError
(这 会由 Rails 完成),而是从 Grape 的输出逐字返回默认的“不匹配路由”处理程序。有点无益的是,即使指定了 format :json
,此处理程序也会生成一个普通的“未找到”消息,这就是您所看到的行为。
解决方案是提供您自己的匹配器来捕获您的 V1
和 V2
模块中指定的路线以外的路线。尝试将此代码添加到 API::Base
下面 三个 mount
语句:
# Generate a properly formatted 404 error for all unmatched routes except '/'
route :any, '*path' do
error!({ error: 'Not Found',
detail: "No such route '#{request.path}'",
status: '404' },
404)
end
# Generate a properly formatted 404 error for '/'
route :any do
error!({ error: 'Not Found',
detail: "No such route '#{request.path}'",
status: '404' },
404)
end
当然,您需要编辑每个 route
block 的正文以满足您的需要。
出于某种原因,这两个 block 似乎都是捕获所有不匹配的路线所必需的;这是 Grape 的匹配器语法或我的理解的限制。
关于api - Rails 4 葡萄 API ActionController::RoutingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486871/
rails 服务器说 Started POST "/user/1/follow" ActionController::RoutingError (No route matches [POST] "/u
简单:一方面 Angularjs 在服务器上运行 'use strict' ### Sevices ### angular.module('app.services', []) .factory 'C
我的道路有问题,我不明白他为什么告诉我“Dashboard::TasksController”因为在我的 Controller 文件中包含文件 dashbaord/tasks.rb class Tas
我正在尝试创建一个仅响应 json 的简单注册模型: class Api::RegistrationController "OK" else render :status =>
所以...第一次向 StackOverflow 提问... 我按照 Rails Guides 和 RailsApps 项目将现有的 Rails 4.2.5 应用程序(使用 Ruby 2.2.4)转换为
我正在研究一个使用 unicorn 和 rails 的教程。我是 Rails 的新手,出于本教程的目的,我为项目所做的所有工作都是 bundle exec rails new rails-starte
在预编译 Assets 后,尝试使用 Assets 管道在生产模式下测试部署一个简单的 Rails 3.1 应用程序。现在使用 JRuby 和 WEBrick 1.3.1;下一步计划是在 JBoss
我试图从 ActionController::RoutingError 营救我无法让它工作。我尝试了几乎所有可以在网上找到的东西,包括 rescue_from ActionController::Ro
我正在使用 rails 创建一个 rest api,并使用 ember 数据从远程 ember 应用程序进行调用。我在什么应该是 GET 请求时收到此路由错误。我如何创建一个 OPTIONS 方法,我
更新 : 这是因为文件名拼写错误 正确的:~/sample_app/app/controllers/microposts_controller.rb 不正确:~/sample_app/app/cont
rails 3、paperclip 和 s2 bucket 发生路由错误。我想在我的应用程序中附加视频。但它失败并抛出以下内容 ActionController::RoutingError (No r
我在 app/controllers/v1/white_label_api 中创建 Controller class Api::WhiteLabel::V1::BaseController < App
rails 4.2.4, ruby 2.1.7 我在 lib/目录中有一个模块。 lib/BLL/user_feed.rb module BLL class UserFeed def ini
好的,我已经看到了很多关于在应用程序中使用命名空间时如何让 Devise 工作的不同答案,但没有一个对我有用。 我将我的应用程序分成三个命名空间 主页(公共(public)登录页面) 帐户(用户登录的
如何禁用 ActionController::RoutingError 错误的日志记录? 我的生产日志文件中充满了机器人,它们正在搜索phpMyAdmin和其他内容。 然后忽略了实际的应用程序错误。
我检查了常见的拼写错误或错误位置的文件。毫无疑问,它会很简单——但我就是看不到它。我错过了什么? controllers/admin/top_10_pages_controller.rb class
我正在尝试让 Grape API 以 json 格式回答其所有动词。问题是我无法回答 json 格式的路由错误。我什至无法挽救 ActionController::RoutingError。 我已阅读
我正在使用 jquery gem 并安装了所有可以删除质子的东西。 我收到此错误,但我不明白为什么? ActionController::RoutingError (No route matches
有没有办法更改 ActionController::RoutingError 的日志级别? 我想将其从错误更改为警告。 google了一下,没找到解决办法。。。 我只想将路由错误显示为警告。但不会更改
我创建了一个名为“post”的自定义 RESTful 操作。它作为一个名为 post 的(公共(public))方法出现在 TransactionsController 中。 resources :t
我是一名优秀的程序员,十分优秀!