我有一个动作,如果status
返回404,则调用一些api,我想从catch返回它。我直接在反应组件中调用此操作,但不从操作中恢复它。
行动
const someAction = (tagId, token) => dispatch => {
dispatch({
type: TagAssignmentActionTypes.TagAssignmentChanged,
})
let status, assignee, response
try {
response = await DeviceApi.checkTagAssignment(tagId, token)
assignee = response.result.assignee
status = response.result.status
return response.result
} catch (e) {
console.log(e, 'eeeeeeee')
if (e && e.status === httpStatusCode.notFound)
status = TagStatus.NotFound
return status
}
dispatch({
type: TagAssignmentActionTypes.TagAssignmentChanged,
status,
assignee,
response,
})
}
反应组件中的功能
lookupComplete = (tagId = this.state.tagId) => this.setState({tagId}, async () => {
let person, status
let result = await this.props.someAction(parseInt(tagId, 16), this.props.accessToken)
console.log(result.status, 'status')
status = result.status
person = this.props.persons[result.assignee]
person
? this.triggerTransition(transitions.ClickCheckTag, {person, status}) : this.triggerTransition(transitions.Free)
})
如果出现错误,如何从catch返回状态?
答案 0 :(得分:0)
在您的try语句中,您需要检查状态是否等于404,然后抛出错误。然后你的catch语句就会执行。
try {
response = await DeviceApi.checkTagAssignment(tagId, token);
assignee = response.result.assignee
status = response.result.status;
if (status === httpStatusCode.notFound){
var error404 = {
message: "not found",
status: httpStatusCode.notFound
};
throw error404 ;
} else {
return response.result;
}
}