From 8bc9fa663c85d006d0b4687e36b2879728d83857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Silvestre=20Padr=C3=B3s?= Date: Wed, 28 Feb 2018 08:47:39 +0100 Subject: [PATCH 1/5] Allow not parsing errors as JSON --- src/Servant/JS/Internal.hs | 4 ++++ src/Servant/JS/Vanilla.hs | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Servant/JS/Internal.hs b/src/Servant/JS/Internal.hs index 36b31c6..789a49c 100644 --- a/src/Servant/JS/Internal.hs +++ b/src/Servant/JS/Internal.hs @@ -77,6 +77,8 @@ data CommonGeneratorOptions = CommonGeneratorOptions -- ^ namespace on which we define the foreign function (empty mean local var) , urlPrefix :: Text -- ^ a prefix we should add to the Url in the codegen + , parseErrors :: Bool + -- ^ if false the error callback won't try to parse error responses } -- | Default options. @@ -89,6 +91,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions -- > , errorCallback = "onError" -- > , moduleName = "" -- > , urlPrefix = "" +-- > , parseErrors = True -- > } -- @ defCommonGeneratorOptions :: CommonGeneratorOptions @@ -100,6 +103,7 @@ defCommonGeneratorOptions = CommonGeneratorOptions , errorCallback = "onError" , moduleName = "" , urlPrefix = "" + , parseErrors = True } -- | Attempts to reduce the function name provided to that allowed by @'Foreign'@. diff --git a/src/Servant/JS/Vanilla.hs b/src/Servant/JS/Vanilla.hs index b18c413..0b1c3ae 100644 --- a/src/Servant/JS/Vanilla.hs +++ b/src/Servant/JS/Vanilla.hs @@ -44,7 +44,8 @@ generateVanillaJSWith opts req = "\n" <> <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(e); }\n" <> " if (res) " <> onSuccess <> "(res);\n" <> " } else {\n" - <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(e); }\n" + <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(" <> + if parseErrors opts then "e" else "xhr.reposnseText" <> "); }\n" <> " if (res) " <> onError <> "(res);\n" <> " }\n" <> " }\n" From bb17c6b7d95df4948884bd53a406c122f70077cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Silvestre=20Padr=C3=B3s?= Date: Wed, 28 Feb 2018 09:03:39 +0100 Subject: [PATCH 2/5] Change logic --- src/Servant/JS/Internal.hs | 8 ++++---- src/Servant/JS/Vanilla.hs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Servant/JS/Internal.hs b/src/Servant/JS/Internal.hs index 789a49c..94fa1d5 100644 --- a/src/Servant/JS/Internal.hs +++ b/src/Servant/JS/Internal.hs @@ -77,8 +77,8 @@ data CommonGeneratorOptions = CommonGeneratorOptions -- ^ namespace on which we define the foreign function (empty mean local var) , urlPrefix :: Text -- ^ a prefix we should add to the Url in the codegen - , parseErrors :: Bool - -- ^ if false the error callback won't try to parse error responses + , ignoreErrorParsingErrors :: Bool + -- ^ if a JSON request receives a non-JSON error response, report the plain text original error instead of a JSON parsing error } -- | Default options. @@ -91,7 +91,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions -- > , errorCallback = "onError" -- > , moduleName = "" -- > , urlPrefix = "" --- > , parseErrors = True +-- > , ignoreErrorParsingErrors = False -- > } -- @ defCommonGeneratorOptions :: CommonGeneratorOptions @@ -103,7 +103,7 @@ defCommonGeneratorOptions = CommonGeneratorOptions , errorCallback = "onError" , moduleName = "" , urlPrefix = "" - , parseErrors = True + , ignoreErrorParsingErrors = False } -- | Attempts to reduce the function name provided to that allowed by @'Foreign'@. diff --git a/src/Servant/JS/Vanilla.hs b/src/Servant/JS/Vanilla.hs index 0b1c3ae..ce39cac 100644 --- a/src/Servant/JS/Vanilla.hs +++ b/src/Servant/JS/Vanilla.hs @@ -45,7 +45,7 @@ generateVanillaJSWith opts req = "\n" <> <> " if (res) " <> onSuccess <> "(res);\n" <> " } else {\n" <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(" <> - if parseErrors opts then "e" else "xhr.reposnseText" <> "); }\n" + if parseErrors opts then "e" else "xhr.responseText" <> "); }\n" <> " if (res) " <> onError <> "(res);\n" <> " }\n" <> " }\n" From 159ec6466fdc2e7c7a3c4a043de9cbc07c123656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Silvestre=20Padr=C3=B3s?= Date: Wed, 28 Feb 2018 09:06:57 +0100 Subject: [PATCH 3/5] Fix --- src/Servant/JS/Vanilla.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Servant/JS/Vanilla.hs b/src/Servant/JS/Vanilla.hs index ce39cac..f0adbb4 100644 --- a/src/Servant/JS/Vanilla.hs +++ b/src/Servant/JS/Vanilla.hs @@ -45,7 +45,7 @@ generateVanillaJSWith opts req = "\n" <> <> " if (res) " <> onSuccess <> "(res);\n" <> " } else {\n" <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(" <> - if parseErrors opts then "e" else "xhr.responseText" <> "); }\n" + if ignoreErrorParsingErrors opts then "xhr.responseText" else "e" <> "); }\n" <> " if (res) " <> onError <> "(res);\n" <> " }\n" <> " }\n" From ab6f005fc8b2fe88c5e0d7e09141625293d8dca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Silvestre=20Padr=C3=B3s?= Date: Wed, 28 Feb 2018 09:10:13 +0100 Subject: [PATCH 4/5] Fix --- src/Servant/JS/Vanilla.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Servant/JS/Vanilla.hs b/src/Servant/JS/Vanilla.hs index f0adbb4..1b7b1b2 100644 --- a/src/Servant/JS/Vanilla.hs +++ b/src/Servant/JS/Vanilla.hs @@ -45,7 +45,7 @@ generateVanillaJSWith opts req = "\n" <> <> " if (res) " <> onSuccess <> "(res);\n" <> " } else {\n" <> " try { res = JSON.parse(xhr.responseText); } catch (e) { " <> onError <> "(" <> - if ignoreErrorParsingErrors opts then "xhr.responseText" else "e" <> "); }\n" + (if ignoreErrorParsingErrors opts then "xhr.responseText" else "e") <> "); }\n" <> " if (res) " <> onError <> "(res);\n" <> " }\n" <> " }\n" From 865e2ec264599e328f0ce86e7dfbb8684a75bea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Silvestre=20Padr=C3=B3s?= Date: Tue, 27 Nov 2018 23:48:37 +0100 Subject: [PATCH 5/5] fix --- src/Servant/JS/Vanilla.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Servant/JS/Vanilla.hs b/src/Servant/JS/Vanilla.hs index c00cc95..b9eb073 100644 --- a/src/Servant/JS/Vanilla.hs +++ b/src/Servant/JS/Vanilla.hs @@ -36,7 +36,7 @@ generateVanillaJSWith opts req = "\n" <> <> " xhr.open('" <> decodeUtf8 method <> "', " <> url <> ", true);\n" <> reqheaders <> " xhr.setRequestHeader('Accept', 'application/json');\n" - <> (if isJust (req ^. reqBody) && (req ^. reqBodyContentType == ReqBodyJSON) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "") + <> (if isJust (req ^. reqBody) && (req ^. _reqBodyContentType == ReqBodyJSON) then " xhr.setRequestHeader('Content-Type', 'application/json');\n" else "") <> " xhr.onreadystatechange = function () {\n" <> " var res = null;\n" <> " if (xhr.readyState === 4) {\n" @@ -82,7 +82,7 @@ generateVanillaJSWith opts req = "\n" <> dataBody = if isJust (req ^. reqBody) - then if (req ^. reqBodyContentType == ReqBodyJSON) then "JSON.stringify(body)" else "body" + then if (req ^. _reqBodyContentType == ReqBodyJSON) then "JSON.stringify(body)" else "body" else "null"