Skip to content
This repository has been archived by the owner on Apr 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #172 from Gizra/97-error-handling
Browse files Browse the repository at this point in the history
Error handling and Translate
  • Loading branch information
amitaibu authored Oct 9, 2017
2 parents 7783841 + 82949dc commit e5d607b
Show file tree
Hide file tree
Showing 29 changed files with 3,235 additions and 691 deletions.
1 change: 1 addition & 0 deletions ci-scripts/docker_files/LocalConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Pusher.Model exposing (Cluster(..), PusherAppKey)
local : Model
local =
{ backendUrl = "http://server.local"
, debug = True
, name = "local"
, pusherKey = PusherAppKey "" UsEast1
}
Expand Down
9 changes: 7 additions & 2 deletions ci-scripts/docker_files/run_webdriverio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ for SPEC in test/specs/*js; do
SPEC_BASENAME=$(echo "$SPEC" | cut -d '/' -f 3 | cut -d '.' -f 1)
sed "s/<<SPECNAME>>/$SPEC_BASENAME/" < $WDIO_CONF.orig > "$WDIO_CONF"
for i in $(seq 3); do
./node_modules/.bin/wdio "$WDIO_CONF" --spec "$SPEC"
WDIO_RET=$?
./node_modules/.bin/wdio "$WDIO_CONF" --spec "$SPEC" 2>&1 | tee /tmp/"$SPEC_BASENAME"-"$i".txt
WDIO_CMD_RET=$?
if grep -E -i "(Debug errors appears, due to an error)" /tmp/"$SPEC_BASENAME"-"$i".txt; then
WDIO_RET=1
else
WDIO_RET=$WDIO_CMD_RET
fi
if [[ "$WDIO_RET" -eq 0 ]]; then
# We give 3 chances to complete
# but of course we quit on first success
Expand Down
10 changes: 8 additions & 2 deletions client/src/elm/App/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ module App.Model
import App.PageType exposing (Page(..))
import Config.Model
import Date exposing (Date)
import Pages.Login.Model exposing (emptyModel, Model)
import Error.Model exposing (Error)
import ItemManager.Model exposing (Model, emptyModel)
import Pages.Login.Model exposing (Model, emptyModel)
import Pusher.Model
import RemoteData exposing (RemoteData(..), WebData)
import ItemManager.Model exposing (emptyModel, Model)
import Time exposing (Time)
import Translate exposing (Language(English))
import User.Model exposing (..)


Expand All @@ -36,6 +38,8 @@ type alias Model =
, activePage : Page
, config : RemoteData String Config.Model.Model
, currentDate : Date
, errors : List Error
, language : Language
, offline : Bool
, pageLogin : Pages.Login.Model.Model
, pageItem : ItemManager.Model.Model
Expand All @@ -62,6 +66,8 @@ emptyModel =
, activePage = Login
, config = NotAsked
, currentDate = Date.fromTime 0
, errors = []
, language = English
, offline = False
, pageLogin = Pages.Login.Model.emptyModel
, pageItem = ItemManager.Model.emptyModel
Expand Down
73 changes: 54 additions & 19 deletions client/src/elm/App/Test.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import App.Model exposing (..)
import App.PageType exposing (Page(..))
import App.Update exposing (..)
import App.View exposing (view)
import Error.Utils exposing (plainError)
import Expect
import Fixtures exposing (exampleModel)
import Http
import Test exposing (describe, test, Test)
import Test.Html.Query as Query
import Test.Html.Selector exposing (text, tag)
import Maybe.Extra exposing (unwrap)
import RemoteData exposing (RemoteData(..))
import Test exposing (Test, describe, test)
import Test.Html.Query as Query
import Test.Html.Selector exposing (class, tag, text)


setActivePageTest : Test
Expand All @@ -35,43 +38,75 @@ setActivePageTest =

getPageAsAnonymous : Page -> Page
getPageAsAnonymous page =
update (SetActivePage page) { emptyModel | user = Failure Http.NetworkError }
update (SetActivePage page) { exampleModel | user = Failure Http.NetworkError }
|> Tuple.first
|> .activePage


getPageAsAuthenticated : Page -> Page
getPageAsAuthenticated page =
let
dummyUser =
{ id = 100
, name = "Foo"
, avatarUrl = "https://example.com"
, pusherChannel = "general"
}

model =
{ emptyModel | user = Success dummyUser }
in
update (SetActivePage page) model
|> Tuple.first
|> .activePage
update (SetActivePage page) exampleModel
|> Tuple.first
|> .activePage


viewConfigErrorTest : Test
viewConfigErrorTest =
describe "Config error view"
[ test "Correct error message appears when config has errored" <|
\() ->
view { emptyModel | config = Failure "some error" }
view { exampleModel | config = Failure "some error" }
|> Query.fromHtml
|> Query.has [ text "Configuration error" ]
]


viewDebugErrorTest : Test
viewDebugErrorTest =
let
errors =
unwrap [] (\error -> [ error ]) (plainError "module" "location" "error message")

modelWithDebug =
{ exampleModel
| config = RemoteData.map (\config -> { config | debug = True }) exampleModel.config
, errors = errors
}

modelWithoutDebug =
{ exampleModel
| config = RemoteData.map (\config -> { config | debug = False }) exampleModel.config
, errors = errors
}
in
describe "Development errors appear on Debug mode"
[ test "no errors in enabled debug mode" <|
\() ->
view { modelWithDebug | errors = [] }
|> Query.fromHtml
|> Query.hasNot [ class "debug-errors" ]
, test "no errors in enabled debug mode" <|
\() ->
view { modelWithoutDebug | errors = [] }
|> Query.fromHtml
|> Query.hasNot [ class "debug-errors" ]
, test "errors in enabled debug mode" <|
\() ->
view modelWithDebug
|> Query.fromHtml
|> Query.has [ class "debug-errors" ]
, test "errors in disabled debug mode" <|
\() ->
view modelWithoutDebug
|> Query.fromHtml
|> Query.hasNot [ class "debug-errors" ]
]


all : Test
all =
describe "App tests"
[ setActivePageTest
, viewConfigErrorTest
, viewDebugErrorTest
]
24 changes: 14 additions & 10 deletions client/src/elm/App/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ port module App.Update exposing (init, update, subscriptions)

import App.Model exposing (..)
import App.PageType exposing (Page(..))
import App.Utils exposing (handleErrors)
import Config
import Date
import Dict
Expand All @@ -21,9 +22,6 @@ import User.Model exposing (..)
init : Flags -> ( Model, Cmd Msg )
init flags =
let
user =
NotAsked

( config, cmds, activePage ) =
case (Dict.get flags.hostname Config.configs) of
Just config ->
Expand Down Expand Up @@ -56,7 +54,7 @@ init flags =
| accessToken = flags.accessToken
, activePage = activePage
, config = config
, user = user
, user = NotAsked
}
, Cmd.batch cmds
)
Expand Down Expand Up @@ -101,7 +99,7 @@ update msg model =
case model.user of
Success user ->
let
( val, cmds, redirectPage ) =
( val, cmds, redirectPage, maybeError ) =
ItemManager.Update.update model.currentDate backendUrl model.accessToken user subMsg model.pageItem

modelUpdated =
Expand All @@ -114,8 +112,11 @@ update msg model =
)
redirectPage
|> Maybe.withDefault ( modelUpdated, Cmd.none )

modelUpdatedWithError =
handleErrors maybeError modelUpdatedWithSetPage
in
( modelUpdatedWithSetPage
( modelUpdatedWithError
, Cmd.batch
[ Cmd.map MsgItemManager cmds
, setPageCmds
Expand All @@ -140,7 +141,7 @@ update msg model =

PageLogin msg ->
let
( val, cmds, ( webDataUser, accessToken ) ) =
( val, cmds, ( webDataUser, accessToken ), maybeError ) =
Pages.Login.Update.update backendUrl msg model.pageLogin

( pusherModelUpdated, pusherLoginCmd ) =
Expand All @@ -154,7 +155,7 @@ update msg model =
, user = webDataUser
}

( modelWithRedirect, setActivePageCmds ) =
( modelUpdatedWithSetPage, setPageCmds ) =
case webDataUser of
-- If user was successfuly fetched, reditect to my
-- account page.
Expand All @@ -178,12 +179,15 @@ update msg model =

_ ->
modelUpdated ! []

modelUpdatedWithError =
handleErrors maybeError modelUpdatedWithSetPage
in
( modelWithRedirect
( modelUpdatedWithError
, Cmd.batch
[ Cmd.map PageLogin cmds
, accessTokenPort accessToken
, setActivePageCmds
, setPageCmds
, pusherLoginCmd
]
)
Expand Down
29 changes: 29 additions & 0 deletions client/src/elm/App/Utils.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module App.Utils
exposing
( handleErrors
)

import App.Model exposing (Model, Msg(..))
import Error.Model exposing (Error)
import Error.Utils exposing (debugLog)
import Maybe.Extra exposing (unwrap)


{-| If there was an error, add it to the top of the list,
and send to console.
-}
handleErrors : Maybe Error -> Model -> Model
handleErrors maybeError model =
let
errors =
unwrap model.errors
(\error ->
let
_ =
debugLog error
in
error :: model.errors
)
maybeError
in
{ model | errors = errors }
Loading

0 comments on commit e5d607b

Please sign in to comment.