我正在使用redux thunk来获取动作中的一些数据
from farmhelper import *
from random import *
# Field for holding the crops.
class Field():
def rain(self):
for i in range(len(self.plants)):
self.plants[i].water()
def __init__(self, size, crop):
self.plants = [0] * size
for i in range(size):
self.plants[i] = crop()
class Combine():
def harvest(self, field):
quantity = 0
for i in range(len(field.plants)):
quantity += field.plants[i].harvest()
return quantity
# Create fields with 10,000 of each crop
cornField = Field(10000, Corn)
wheatField = Field(10000, Wheat)
# Create irrigators for each field
cornIrrigator = Irrigator(20000)
wheatIrrigator = Irrigator(500)
# Create a combine for harvesting
combine = Combine()
# 90 days ~3 months of growth
for i in range(90):
# Low chance of rain
if randint(0, 100) > 95:
print("It rained")
cornField.rain()
wheatField.rain()
# Always run the irrigators. Since they are never
# refilled they will quickly run out
cornIrrigator.irrigate(cornField)
wheatIrrigator.irrigate(wheatField)
# Gather the crops - DONE
earsOfCorn = combine.harvest(cornField)
headsOfWheat = combine.harvest(wheatField)
# Print the result - DONE
print("Grew", earsOfCorn, "ears of corn")
print("and", headsOfWheat, "heads of wheat")
我的快速服务器出现错误时会回复'some error'
function handleErrors(response) {
console.log(response)
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
export const something = (var) => dispatch => {
fetch(`${url}/something`, {credentials: 'include'})
.then(handleErrors)
.then(res => res.json())
.then(res =>
dispatch({
type: SOMETHING,
payload: res
})
)
.catch(error =>
dispatch({
type: ERROR,
payload: error
})
)
当它获取并且是错误(500)时,其消息是通用的“内部服务器错误”。
如何获取fetch中的“some error”?
答案 0 :(得分:3)
不确定handleError
中有什么内容。提取错误消息的一种方法是这样的
fetch(url)
.then(res => {
// Check if response has errors
if (res.ok) {
// No errors
return res.json();
} else {
// Has errors, since res.json() returns a Promise, we
// chain a then here to get the value and return the err value
// as Promise rejection so that it will go to the
// catch handler
return res.json().then(err => Promise.reject(err));
// this could also be
// return res.json().then(err => throw Error(err));
}
})
.then(json => {
// dispatch success
})
.catch(err => {
// dispatch error
});