Skip to content

Commit 1653bdb

Browse files
committed
feat(error-handling): display error-message upon server fails
Catch all server errors including everything else thrown from redux dispatch. Also, utilize built-in bootstrap alerts for styling: http://getbootstrap.com/components/#alerts
1 parent e28e5a3 commit 1653bdb

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

lib/actions/api.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,18 @@ export function planTrip (customOtpQueryBuilder) {
2828
const queryBuilderFn = customOtpQueryBuilder || otpState.config.customOtpQueryBuilder || constructPlanQuery
2929
const url = queryBuilderFn(otpState.config.api, otpState.currentQuery)
3030
// setURLSearch(url)
31-
let serverResponse
3231
fetch(url)
3332
.then(response => {
34-
serverResponse = response
35-
return response.json()
36-
})
37-
.then(json => {
38-
if (serverResponse.status >= 400) {
39-
dispatch(receivedPlanError({
40-
body: json,
41-
status: serverResponse.status
42-
}))
43-
} else {
44-
dispatch(receivedPlanResponse(json))
33+
if (response.status >= 400) {
34+
const error = new Error('Received error from server')
35+
error.response = response
36+
throw error
4537
}
38+
return response.json()
4639
})
40+
.then(json => dispatch(receivedPlanResponse(json)))
4741
.catch((err) => {
48-
throw err
42+
dispatch(receivedPlanError(err))
4943
})
5044
}
5145
}

lib/components/form/error-message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class ErrorMessage extends Component {
1313
if (!error) return null
1414

1515
return (
16-
<div className='error'>
16+
<div className='alert alert-danger error'>
1717
<div className='header'>Error!</div>
18-
<div className='message'>{error.msg}</div>
18+
<div className='message'>{error.message}</div>
1919
</div>
2020
)
2121
}

lib/components/form/form.css

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
.otp .error {
2-
margin-bottom: 16px;
3-
padding: 10px;
4-
background-color: #f5f5f5;
5-
border: 1px solid #e3e3e3;
6-
border-radius: 4px
7-
}
8-
91
.otp .error > .header {
102
font-size: 16px;
113
padding-bottom: 4px;
12-
border-bottom: 1px solid black;
13-
margin-bottom: 6px;
4+
border-bottom: 1px solid #dbbec3;
145
}
156

167
.otp .error > .header > .title {

lib/reducers/create-otp-reducer.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function createOtpReducer (config, initialQuery) {
3131
}
3232

3333
return (state = initialState, action) => {
34+
const latestSearchIndex = state.searches.length - 1
3435
switch (action.type) {
3536
case 'PLAN_REQUEST':
3637
return update(state, {
@@ -45,18 +46,23 @@ function createOtpReducer (config, initialQuery) {
4546
})
4647
case 'PLAN_ERROR':
4748
return update(state, {
48-
searches: {[state.searches.length - 1]: {
49+
searches: {[latestSearchIndex]: {
50+
planResponse: {
51+
$set: {
52+
error: action.payload
53+
}
54+
},
4955
pending: {$set: false}
50-
}}
56+
}},
57+
activeSearch: { $set: latestSearchIndex }
5158
})
5259
case 'PLAN_RESPONSE':
53-
const latestIndex = state.searches.length - 1
5460
return update(state, {
55-
searches: {[latestIndex]: {
61+
searches: {[latestSearchIndex]: {
5662
planResponse: {$set: action.payload},
5763
pending: {$set: false}
5864
}},
59-
activeSearch: { $set: state.searches.length - 1 }
65+
activeSearch: { $set: latestSearchIndex }
6066
})
6167
case 'SET_ACTIVE_ITINERARY':
6268
if (state.activeSearch !== null) {

0 commit comments

Comments
 (0)