【问题标题】:Ember Data 403 Get Response Not Entering Error ActionEmber 数据 403 获取响应未输入错误操作
【发布时间】:2018-10-02 16:37:20
【问题描述】:

所以我正在尝试保护 API 端点,以便只有拥有对象的用户才能获取有关该对象的详细信息。

API 正在返回 403 响应,以及符合 JSONAPI 的 json 负载:

{
  "errors": [
    { "status": "403", "title": "Forbidden", "detail": "You are not authorized to access this resource" }

  ]
}

太棒了!因此,在这一点上,我试图连接到路由的生命周期,以便在用户尝试查看属于其他人的资源时将其转换到主页。

https://www.emberjs.com/api/ember-data/release/classes/DS.AdapterError 暗示这就像向路由添加错误操作并采取任何措施来处理它一样简单。

//routes/my-resource.js
export default Route.extend({
  model(params) {
    this.store.findRecord('my-resource', params.id)
  }
});

//routes/application.js
export default Route.extend(ApplicationRouteMixin, {
  actions: {
    error(error, transition) {
      debugger
    }
  }
});

我从来没有点击过这个调试器,因为错误操作从来没有被调用过。我已经在应用程序路由级别和特定路由级别进行了尝试。相反,路由正常加载,但控制台中有一个通用的 Ember 错误(粘贴在下面),显然资源不在存储中。

我有点不知道该尝试什么。我在适配器级别挂钩了 handleResponse 并尝试手动发出 DS.ForbiddenError,但路由仍然没有调用错误挂钩。

Uncaught ErrorClass {isAdapterError: true, stack: "Error: Ember Data Request GET /api/my-resource… (http://localhost:4200/assets/vendor.js:3609:31)", description: undefined, fileName: undefined, lineNumber: undefined, …}code: undefineddescription: undefinederrors: [{…}]fileName: undefinedisAdapterError: truelineNumber: undefinedmessage: "Ember Data Request GET /api/my-resource/2 returned a 403↵Payload (Empty Content-Type)↵[object Object]"name: "Error"number: undefinedstack: "Error: Ember Data Request GET /api/my-resource/2 returned a 403↵Payload (Empty Content-Type)↵[object Object]↵    at ErrorClass.EmberError (http://localhost:4200/assets/vendor.js:13638:31)↵    at ErrorClass.AdapterError (http://localhost:4200/assets/vendor.js:90664:17)↵    at new ErrorClass (http://localhost:4200/assets/vendor.js:90682:24)↵    at Class.handleResponse (http://localhost:4200/assets/vendor.js:103063:18)↵    at Class.handleResponse (http://localhost:4200/assets/vendor.js:110305:19)↵    at Class.superWrapper [as handleResponse] (http://localhost:4200/assets/vendor.js:53436:28)↵    at ajaxError (http://localhost:4200/assets/vendor.js:103345:25)↵    at ajaxErrorHandler (http://localhost:4200/assets/vendor.js:103372:12)↵    at Class.hash.error (http://localhost:4200/assets/vendor.js:103140:23)↵    at fire (http://localhost:4200/assets/vendor.js:3609:31)"__proto__: EmberError
onerrorDefault @ rsvp.js:24
trigger @ rsvp.js:66
(anonymous) @ rsvp.js:886
invoke @ backburner.js:247
flush @ backburner.js:167
flush @ backburner.js:326
_end @ backburner.js:748
end @ backburner.js:513
_run @ backburner.js:793
_join @ backburner.js:769
join @ backburner.js:567
join @ index.js:164
hash.error @ rest.js:880
fire @ jquery.js:3268
fireWith @ jquery.js:3398
done @ jquery.js:9307
(anonymous) @ jquery.js:9548
load (async)
send @ jquery.js:9567
ajax @ jquery.js:9206
_ajaxRequest @ rest.js:893
_ajax @ rest.js:913
(anonymous) @ rest.js:883
initializePromise @ rsvp.js:397
Promise @ rsvp.js:877
ajax @ rest.js:873
findRecord @ rest.js:436
Ember.RSVP.Promise.resolve.then @ -private.js:9195
tryCatcher @ rsvp.js:200
invokeCallback @ rsvp.js:372
(anonymous) @ rsvp.js:436
(anonymous) @ rsvp.js:14
invoke @ backburner.js:247
flush @ backburner.js:167
flush @ backburner.js:326
_end @ backburner.js:748
end @ backburner.js:513
_run @ backburner.js:793
_join @ backburner.js:769
join @ backburner.js:567
join @ index.js:164
(anonymous) @ index.js:265
mightThrow @ jquery.js:3534
process @ jquery.js:3602
setTimeout (async)
(anonymous) @ jquery.js:3640
fire @ jquery.js:3268
fireWith @ jquery.js:3398
fire @ jquery.js:3406
fire @ jquery.js:3268
fireWith @ jquery.js:3398
ready @ jquery.js:3878
completed @ jquery.js:3888

【问题讨论】:

  • FWIW 我也在使用 ember-simple-auth,但在该代码库中找不到任何会干扰内置错误处理的内容

标签: ember.js ember-data


【解决方案1】:

问题是我没有返回模型钩子的承诺:

model(params) {
   return this.store.findRecord('my-resource', params.id)
}

【讨论】:

    猜你喜欢
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多