Skip to content

Allow not parsing errors as JSON #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/Servant/JS/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -92,6 +94,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions
-- > , errorCallback = "onError"
-- > , moduleName = ""
-- > , urlPrefix = ""
-- > , ignoreErrorParsingErrors = False
-- > }
-- @
defCommonGeneratorOptions :: CommonGeneratorOptions
Expand All @@ -103,6 +106,7 @@ defCommonGeneratorOptions = CommonGeneratorOptions
, errorCallback = "onError"
, moduleName = ""
, urlPrefix = ""
, ignoreErrorParsingErrors = False
}

-- | Attempts to reduce the function name provided to that allowed by @'Foreign'@.
Expand Down
7 changes: 4 additions & 3 deletions src/Servant/JS/Vanilla.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"


Expand Down