diff --git a/src/Servant/JS/Internal.hs b/src/Servant/JS/Internal.hs index 91e8e8c..451fc5e 100644 --- a/src/Servant/JS/Internal.hs +++ b/src/Servant/JS/Internal.hs @@ -80,6 +80,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 + , 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. @@ -92,6 +94,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions -- > , errorCallback = "onError" -- > , moduleName = "" -- > , urlPrefix = "" +-- > , ignoreErrorParsingErrors = False -- > } -- @ defCommonGeneratorOptions :: CommonGeneratorOptions @@ -103,6 +106,7 @@ defCommonGeneratorOptions = CommonGeneratorOptions , errorCallback = "onError" , moduleName = "" , urlPrefix = "" + , 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 72e6df0..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" @@ -46,7 +46,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 ignoreErrorParsingErrors opts then "xhr.responseText" else "e") <> "); }\n" <> " if (res) " <> onError <> "(res);\n" <> " }\n" <> " }\n" @@ -81,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"