Skip to content

Commit

Permalink
improve: show possible reasons when url fetch fails (swagger-api#4295)
Browse files Browse the repository at this point in the history
* improve: show possible reasons when url fetch fails
* Only check for fail reasons if no HTTP status code
* Check for mixed content errors first
* Harmonise error messages
* Set error source to 'fetch'
* improve: hide loading validator image (swagger-api#4287)
* hide missing validator image
* Render nothing if validator badge has not loaded/received errors
* Revert style changes
* Allow images with data scheme (swagger-api#4305)
* Add UnitTest for image
allows image elements with https scheme
* Test images with data scheme
* Add allowedSchemesByTag
* Fix error  Strings must use doublequote  quotes
* Add empty div (swagger-api#4236)
if there are properties show an empty div
* v3.12.1 (swagger-api#4311)
* v3.12.1
* Rebuild dist for v3.12.1
* Use window wrapper instead of direct reference
* Add trailing period to error messages
* improve readability
  • Loading branch information
scottohara authored and shockey committed Mar 12, 2018
1 parent 49e0466 commit fb7f125
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
39 changes: 38 additions & 1 deletion src/core/plugins/download-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createSelector } from "reselect"
import { Map } from "immutable"
import win from "../window"

export default function downloadUrlPlugin (toolbox) {
let { fn } = toolbox
Expand All @@ -12,6 +13,7 @@ export default function downloadUrlPlugin (toolbox) {
const config = getConfigs()
url = url || specSelectors.url()
specActions.updateLoadingStatus("loading")
errActions.clear({source: "fetch"})
fetch({
url,
loadSpec: true,
Expand All @@ -26,7 +28,10 @@ export default function downloadUrlPlugin (toolbox) {
function next(res) {
if(res instanceof Error || res.status >= 400) {
specActions.updateLoadingStatus("failed")
return errActions.newThrownErr( new Error((res.message || res.statusText) + " " + url) )
errActions.newThrownErr(Object.assign( new Error((res.message || res.statusText) + " " + url), {source: "fetch"}))
// Check if the failure was possibly due to CORS or mixed content
if (!res.status && res instanceof Error) checkPossibleFailReasons()
return
}
specActions.updateLoadingStatus("success")
specActions.updateSpec(res.text)
Expand All @@ -35,6 +40,38 @@ export default function downloadUrlPlugin (toolbox) {
}
}

function checkPossibleFailReasons() {
try {
let specUrl

if("URL" in win ) {
specUrl = new URL(url)
} else {
// legacy browser, use <a href> to parse the URL
specUrl = document.createElement("a")
specUrl.href = url
}

if(specUrl.protocol !== "https:" && win.location.protocol === "https:") {
const error = Object.assign(
new Error(`Possible mixed-content issue? The page was loaded over https:// but a ${specUrl.protocol}// URL was specified. Check that you are not attempting to load mixed content.`),
{source: "fetch"}
)
errActions.newThrownErr(error)
return
}
if(specUrl.origin !== win.location.origin) {
const error = Object.assign(
new Error(`Possible cross-origin (CORS) issue? The URL origin (${specUrl.origin}) does not match the page (${win.location.origin}). Check the server returns the correct 'Access-Control-Allow-*' headers.`),
{source: "fetch"}
)
errActions.newThrownErr(error)
}
} catch (e) {
return
}
}

},

updateLoadingStatus: (status) => {
Expand Down
5 changes: 3 additions & 2 deletions src/standalone/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class StandaloneLayout extends React.Component {
let Container = getComponent("Container")
let Row = getComponent("Row")
let Col = getComponent("Col")
let Errors = getComponent("errors", true)

const Topbar = getComponent("Topbar", true)
const BaseLayout = getComponent("BaseLayout", true)
Expand All @@ -45,8 +46,8 @@ export default class StandaloneLayout extends React.Component {
<div className="info">
<div className="loading-container">
<h4 className="title">Failed to load API definition.</h4>
<p>{lastErrMsg}</p>
</div>
<Errors/>
</div>
</div>
}
{ loadingStatus === "failedConfig" &&
Expand Down

0 comments on commit fb7f125

Please sign in to comment.