灰烬数据403获取响应未输入错误操作

时间:2018-10-02 16:37:20

标签: ember.js ember-data

因此,我正在尝试保护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

1 个答案:

答案 0 :(得分:2)

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

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