diff --git a/src/Applications/Brain/Sources/Processing/Common.elm b/src/Applications/Brain/Sources/Processing/Common.elm index 92305fbc..1d9aa24c 100644 --- a/src/Applications/Brain/Sources/Processing/Common.elm +++ b/src/Applications/Brain/Sources/Processing/Common.elm @@ -111,4 +111,6 @@ translateHttpError service err = "Got a faulty response from this source. Use the developer console to get more info." BadBody response -> - Services.parseErrorResponse service response + response + |> Services.parseErrorResponse service + |> Maybe.withDefault (translateHttpError service <| BadStatus 0) diff --git a/src/Applications/UI.elm b/src/Applications/UI.elm index 104474db..bbed94b5 100644 --- a/src/Applications/UI.elm +++ b/src/Applications/UI.elm @@ -1299,6 +1299,11 @@ translateReply reply model = notificationId = Notifications.id notification + newNotifications = + List.filter + (\n -> Notifications.kind n /= Notifications.Error) + model.notifications + sources = model.sources @@ -1308,6 +1313,9 @@ translateReply reply model = , processingError = Nothing , processingNotificationId = Just notificationId } + + newModel = + { model | notifications = newNotifications, sources = newSources } in [ ( "origin" , Json.Encode.string (Common.urlOrigin model.url) @@ -1319,7 +1327,7 @@ translateReply reply model = |> Json.Encode.object |> Alien.broadcast Alien.ProcessSources |> Ports.toBrain - |> returnWithModel { model | sources = newSources } + |> returnWithModel newModel |> andThen (showNotification notification) RemoveSourceFromCollection args -> diff --git a/src/Library/Common.elm b/src/Library/Common.elm index 0fd43fd3..98cb8554 100644 --- a/src/Library/Common.elm +++ b/src/Library/Common.elm @@ -1,5 +1,6 @@ -module Common exposing (Switch(..), backToIndex, boolFromString, boolToString, queryString, urlOrigin) +module Common exposing (Switch(..), backToIndex, boolFromString, boolToString, queryString, translateHttpResponse, urlOrigin) +import Http import Tuple.Ext as Tuple import Url exposing (Protocol(..), Url) import Url.Builder as Url @@ -51,6 +52,25 @@ queryString = List.map (Tuple.uncurry Url.string) >> Url.toQuery +translateHttpResponse : Http.Response String -> Result Http.Error String +translateHttpResponse response = + case response of + Http.BadUrl_ u -> + Err (Http.BadUrl u) + + Http.Timeout_ -> + Err Http.Timeout + + Http.NetworkError_ -> + Err Http.NetworkError + + Http.BadStatus_ m body -> + Err (Http.BadBody body) + + Http.GoodStatus_ m body -> + Ok body + + urlOrigin : Url -> String urlOrigin { host, port_, path, protocol } = let diff --git a/src/Library/Sources/Services.elm b/src/Library/Sources/Services.elm index 9131222e..f8ad959b 100644 --- a/src/Library/Sources/Services.elm +++ b/src/Library/Sources/Services.elm @@ -101,7 +101,7 @@ makeTree service = WebDav.makeTree -parseErrorResponse : Service -> String -> String +parseErrorResponse : Service -> String -> Maybe String parseErrorResponse service = case service of AmazonS3 -> diff --git a/src/Library/Sources/Services/AmazonS3.elm b/src/Library/Sources/Services/AmazonS3.elm index d8bd8537..57537494 100644 --- a/src/Library/Sources/Services/AmazonS3.elm +++ b/src/Library/Sources/Services/AmazonS3.elm @@ -8,6 +8,7 @@ Resources: -} +import Common import Dict import Http import Sources exposing (Property, SourceData) @@ -139,7 +140,7 @@ makeTree srcData marker currentTime resultMsg = in Http.get { url = url - , expect = Http.expectString resultMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse } @@ -155,7 +156,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/AmazonS3/Parser.elm b/src/Library/Sources/Services/AmazonS3/Parser.elm index 85be68a4..17e6ef3c 100644 --- a/src/Library/Sources/Services/AmazonS3/Parser.elm +++ b/src/Library/Sources/Services/AmazonS3/Parser.elm @@ -52,19 +52,15 @@ markerDecoder = -- ERROR -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse response = response |> decodeString errorMessagesDecoder |> Result.toMaybe - |> Maybe.andThen List.head - |> Maybe.withDefault "Invalid request" -errorMessagesDecoder : Decoder (List String) +errorMessagesDecoder : Decoder String errorMessagesDecoder = string |> single |> path [ "Message" ] - |> list - |> path [ "Error" ] diff --git a/src/Library/Sources/Services/Azure/BlobParser.elm b/src/Library/Sources/Services/Azure/BlobParser.elm index bbb25014..c7d6f390 100755 --- a/src/Library/Sources/Services/Azure/BlobParser.elm +++ b/src/Library/Sources/Services/Azure/BlobParser.elm @@ -50,19 +50,15 @@ markerDecoder = -- ERROR -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse response = response |> decodeString errorMessagesDecoder |> Result.toMaybe - |> Maybe.andThen List.head - |> Maybe.withDefault "Invalid request" -errorMessagesDecoder : Decoder (List String) +errorMessagesDecoder : Decoder String errorMessagesDecoder = string |> single |> path [ "Message" ] - |> list - |> path [ "Error" ] diff --git a/src/Library/Sources/Services/Azure/FileParser.elm b/src/Library/Sources/Services/Azure/FileParser.elm index e9f217cc..34b83adc 100755 --- a/src/Library/Sources/Services/Azure/FileParser.elm +++ b/src/Library/Sources/Services/Azure/FileParser.elm @@ -86,19 +86,15 @@ markerDecoder usedDirectory markerWithDirectories = -- ERROR -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse response = response |> decodeString errorMessagesDecoder |> Result.toMaybe - |> Maybe.andThen List.head - |> Maybe.withDefault "Invalid request" -errorMessagesDecoder : Decoder (List String) +errorMessagesDecoder : Decoder String errorMessagesDecoder = string |> single |> path [ "Message" ] - |> list - |> path [ "Error" ] diff --git a/src/Library/Sources/Services/AzureBlob.elm b/src/Library/Sources/Services/AzureBlob.elm index a9e69f6b..c7916556 100644 --- a/src/Library/Sources/Services/AzureBlob.elm +++ b/src/Library/Sources/Services/AzureBlob.elm @@ -8,6 +8,7 @@ Resources: -} +import Common import Dict import Http import Sources exposing (Property, SourceData) @@ -93,7 +94,7 @@ Or a specific directory in the container. -} makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg -makeTree srcData marker currentTime toMsg = +makeTree srcData marker currentTime resultMsg = let directoryPath = srcData @@ -117,7 +118,7 @@ makeTree srcData marker currentTime toMsg = in Http.get { url = url - , expect = Http.expectString toMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse } @@ -133,7 +134,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/AzureFile.elm b/src/Library/Sources/Services/AzureFile.elm index 4a3ca537..5ecd4402 100644 --- a/src/Library/Sources/Services/AzureFile.elm +++ b/src/Library/Sources/Services/AzureFile.elm @@ -8,6 +8,7 @@ Resources: -} +import Common import Dict import Http import Sources exposing (Property, SourceData) @@ -94,7 +95,7 @@ Or a specific directory in the container. -} makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg -makeTree srcData marker currentTime toMsg = +makeTree srcData marker currentTime resultMsg = let directoryPathFromSrcData = srcData @@ -121,7 +122,7 @@ makeTree srcData marker currentTime toMsg = in Http.get { url = url - , expect = Http.expectString toMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse } @@ -137,7 +138,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/Dropbox.elm b/src/Library/Sources/Services/Dropbox.elm index bfd19cd6..696e2c63 100644 --- a/src/Library/Sources/Services/Dropbox.elm +++ b/src/Library/Sources/Services/Dropbox.elm @@ -125,7 +125,7 @@ Or a specific directory in the bucket. -} makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg -makeTree srcData marker currentTime toMsg = +makeTree srcData marker currentTime resultMsg = let accessToken = Dict.fetch "accessToken" "" srcData @@ -164,7 +164,7 @@ makeTree srcData marker currentTime toMsg = , headers = [ Http.header "Authorization" ("Bearer " ++ accessToken) ] , url = url , body = body - , expect = Http.expectString toMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse , timeout = Nothing , tracker = Nothing } @@ -198,7 +198,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/Dropbox/Parser.elm b/src/Library/Sources/Services/Dropbox/Parser.elm index dd9f37e5..80dbb198 100755 --- a/src/Library/Sources/Services/Dropbox/Parser.elm +++ b/src/Library/Sources/Services/Dropbox/Parser.elm @@ -36,6 +36,8 @@ parseTreeResponse response _ = -- Error -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse response = response + |> decodeString (field "error_summary" string) + |> Result.toMaybe diff --git a/src/Library/Sources/Services/Google.elm b/src/Library/Sources/Services/Google.elm index b24d7d71..5b40e494 100644 --- a/src/Library/Sources/Services/Google.elm +++ b/src/Library/Sources/Services/Google.elm @@ -133,7 +133,7 @@ In this case this means that we will refresh the `access_token`. Or if we don't have an access token yet, get one. -} prepare : String -> SourceData -> Marker -> (Result Http.Error String -> msg) -> Maybe (Cmd msg) -prepare origin srcData _ toMsg = +prepare origin srcData _ resultMsg = let maybeCode = Dict.get "authCode" srcData @@ -166,7 +166,7 @@ prepare origin srcData _ toMsg = (Just << Http.post) { url = url , body = Http.emptyBody - , expect = Http.expectString toMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse } @@ -181,7 +181,7 @@ Or a specific directory in the bucket. -} makeTree : SourceData -> Marker -> Time.Posix -> (Result Http.Error String -> msg) -> Cmd msg -makeTree srcData marker currentTime toMsg = +makeTree srcData marker currentTime resultMsg = let accessToken = Dict.fetch "accessToken" "" srcData @@ -223,7 +223,7 @@ makeTree srcData marker currentTime toMsg = , headers = [ Http.header "Authorization" ("Bearer " ++ accessToken) ] , url = "https://www.googleapis.com/drive/v3/files" ++ queryString , body = Http.emptyBody - , expect = Http.expectString toMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse , timeout = Nothing , tracker = Nothing } @@ -241,7 +241,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/Google/Parser.elm b/src/Library/Sources/Services/Google/Parser.elm index 3de20b64..f4fd88d1 100755 --- a/src/Library/Sources/Services/Google/Parser.elm +++ b/src/Library/Sources/Services/Google/Parser.elm @@ -82,9 +82,11 @@ fileDecoder = --- Error +-- ERROR -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse response = response + |> decodeString (at [ "error", "message" ] string) + |> Result.toMaybe diff --git a/src/Library/Sources/Services/Ipfs.elm b/src/Library/Sources/Services/Ipfs.elm index e65ae322..d0320f5b 100644 --- a/src/Library/Sources/Services/Ipfs.elm +++ b/src/Library/Sources/Services/Ipfs.elm @@ -196,7 +196,7 @@ makeTree srcData marker _ resultMsg = , headers = [] , url = gateway ++ "/api/v0/ls?arg=" ++ ipfsHash ++ "&encoding=json" , body = Http.emptyBody - , resolver = Http.stringResolver ipfsResolver + , resolver = Http.stringResolver Common.translateHttpResponse , timeout = Just (60 * 15) } ) @@ -215,39 +215,16 @@ ipnsResolver response = Http.NetworkError_ -> Err Http.NetworkError - Http.BadStatus_ m body -> - body - |> Json.decodeString (Json.field "Message" Json.string) - |> Result.map Http.BadBody - |> Result.withDefault (Http.BadStatus m.statusCode) - |> Err + Http.BadStatus_ _ body -> + Err (Http.BadBody body) - Http.GoodStatus_ m body -> + Http.GoodStatus_ _ body -> body |> Json.decodeString (Json.field "Path" Json.string) |> Result.map (\hash -> { ipfsHash = hash }) |> Result.mapError (Json.errorToString >> Http.BadBody) -ipfsResolver : Http.Response String -> Result Http.Error String -ipfsResolver response = - case response of - Http.BadUrl_ u -> - Err (Http.BadUrl u) - - Http.Timeout_ -> - Err Http.Timeout - - Http.NetworkError_ -> - Err Http.NetworkError - - Http.BadStatus_ m body -> - Err (Http.BadStatus m.statusCode) - - Http.GoodStatus_ m body -> - Ok body - - {-| Re-export parser functions. -} parsePreparationResponse : String -> SourceData -> Marker -> PrepationAnswer Marker @@ -260,9 +237,9 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = - identity + Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/Ipfs/Parser.elm b/src/Library/Sources/Services/Ipfs/Parser.elm index 8f77fe5d..3108fa00 100755 --- a/src/Library/Sources/Services/Ipfs/Parser.elm +++ b/src/Library/Sources/Services/Ipfs/Parser.elm @@ -1,4 +1,4 @@ -module Sources.Services.Ipfs.Parser exposing (Link, linkDecoder, parseCloudflareDnsResult, parseTreeResponse, treeDecoder) +module Sources.Services.Ipfs.Parser exposing (Link, linkDecoder, parseCloudflareDnsResult, parseErrorResponse, parseTreeResponse, treeDecoder) import Dict import Json.Decode exposing (..) @@ -94,3 +94,14 @@ linkDecoder = (field "Hash" string) (field "Name" string) (field "Type" int) + + + +-- ERRORS + + +parseErrorResponse : String -> Maybe String +parseErrorResponse response = + response + |> decodeString (field "Message" string) + |> Result.toMaybe diff --git a/src/Library/Sources/Services/WebDav.elm b/src/Library/Sources/Services/WebDav.elm index 87348c88..b7b3096c 100644 --- a/src/Library/Sources/Services/WebDav.elm +++ b/src/Library/Sources/Services/WebDav.elm @@ -9,6 +9,7 @@ Resources: -} import Base64 +import Common import Dict import Dict.Ext as Dict import Http @@ -121,7 +122,7 @@ makeTree srcData marker _ resultMsg = , headers = [ Http.header "Authorization" auth, Http.header "Depth" "1" ] , url = url { addAuth = False } srcData directory , body = Http.emptyBody - , expect = Http.expectString resultMsg + , expect = Http.expectStringResponse resultMsg Common.translateHttpResponse , timeout = Nothing , tracker = Nothing } @@ -139,7 +140,7 @@ parseTreeResponse = Parser.parseTreeResponse -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = Parser.parseErrorResponse diff --git a/src/Library/Sources/Services/WebDav/Parser.elm b/src/Library/Sources/Services/WebDav/Parser.elm index db1890c4..003e7815 100644 --- a/src/Library/Sources/Services/WebDav/Parser.elm +++ b/src/Library/Sources/Services/WebDav/Parser.elm @@ -78,6 +78,6 @@ mustBeAudio contentType = -- ERROR -parseErrorResponse : String -> String +parseErrorResponse : String -> Maybe String parseErrorResponse = - identity + Just