我们使用aws和swagger,我们也有getData方法的服务
var getData = () => {
return $http({
method: 'GET',
url: 'myURL',
headers: {
'Authorization': 'token'
}
});
};
我正在尝试将其与下面的代码一起使用
dataservice.getData().then(response => {
console.log(response)
})
.catch((err) => {
console.log(err));
})
对于良好的请求(当我被自动化时)它工作正常,我得到我的数据,但然后令牌无效我在控制台中获取错误whitout状态代码
Object {data: null, status: -1, config: Object, statusText: "", headers: function}
我的目标是获取状态代码401'未自动化'
我们的lamba具有波纹管结构
exports.handler = (event, context, callback) => {
var token = event.authorizationToken;
// Call oauth provider, crack jwt token, etc.
// In this example, the token is treated as the status for simplicity.
switch (token.toLowerCase()) {
case 'allow':
callback(null, generatePolicy('user', 'Allow', event.methodArn));
break;
case 'deny':
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
case 'unauthorized':
callback("Unauthorized"); // Return a 401 Unauthorized response
break;
default:
callback("Error: Invalid token");
}
};
我们从awsdoc
获取此信息答案 0 :(得分:2)
在catch()
中,您可以访问涵盖status
和data
的错误对象,您可以在其中阅读有关失败原因的其他信息。
angular.module('app', []).service('someService', function($http) {
const getData = () => {
return $http({
method: 'GET',
url: 'myURL',
headers: {
'Authorization': 'token'
}
});
};
return {
getData
}
})
describe('Some service', function() {
let someService, $httpBackend
beforeEach(module('app'))
beforeEach(inject((_someService_, _$httpBackend_) => {
someService = _someService_
$httpBackend = _$httpBackend_
}))
it('returns 401 status', () => {
const message = {reason: 'some reason'}
$httpBackend.whenGET('myURL').respond(401, { message })
someService.getData().then((response) => {
expect(response).toBeUndefined()
}).catch(function(error) {
expect(error.status).toBe(401)
expect(error.data.message).toEqual(message)
})
$httpBackend.flush()
})
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.css"></link>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/jasmine-html.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.5.2/boot.js"></script>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="//code.angularjs.org/snapshot/angular-mocks.js"></script>
答案 1 :(得分:0)
请尝试
.success(function (response) {
if (response.token){
}
else{
// do else case
}
})