Skip to content

Commit

Permalink
JSON.parse のエラーハンドリング (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito authored Aug 3, 2023
1 parent eac3450 commit a2acdd0
Show file tree
Hide file tree
Showing 10 changed files with 7,471 additions and 7,073 deletions.
829 changes: 410 additions & 419 deletions apps/tutorial/application.css

Large diffs are not rendered by default.

13,615 changes: 6,981 additions & 6,634 deletions apps/tutorial/application.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions apps/tutorial/application.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions apps/tutorial/serviceworker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/tutorial/serviceworker.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"prettier": "@github/prettier-config",
"dependencies": {
"@types/eslint": "~8.21.0",
"fraction.js": "~4.2",
"pretty-format": "^29.4.2"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@qni/common": "^0.0.86",
"complex.js": "^2.1.1",
"interactjs": "~1.10",
"neverthrow": "^6.0.0",
"pretty-format": "^29.4.2",
"tippy.js": "^6.3.7",
"xstate": "~4.37"
Expand All @@ -53,6 +54,7 @@
"@esm-bundle/chai": "^4.3.4-fix.0",
"@web/dev-server-rollup": "~0.3",
"@web/test-runner": "~0.15",
"eslint-plugin-neverthrow": "^1.1.4",
"postcss-flexbugs-fixes": "^5.0.2",
"rollup-plugin-postcss": "^4.0.2"
},
Expand All @@ -65,7 +67,7 @@
],
"size-limit": [
{
"limit": "122 kB",
"limit": "124 kB",
"path": "dist/index.js"
}
],
Expand Down
27 changes: 24 additions & 3 deletions packages/elements/src/quantum-circuit-element.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Result} from 'neverthrow'
import {CircuitDropzoneElement, isCircuitDropzoneElement} from './circuit-dropzone-element'
import {AntiControlGateElement} from './anti-control-gate-element'
import {BlochDisplayElement} from './bloch-display-element'
Expand Down Expand Up @@ -1082,7 +1083,23 @@ export class QuantumCircuitElement extends HoverableMixin(HTMLElement) {
return
}

const circuit = JSON.parse(json)
let circuit = null

type ParseError = {message: string}
const toParseError = (): ParseError => ({message: 'Parse Error'})
const safeJsonParse = Result.fromThrowable(JSON.parse, toParseError)

const res = safeJsonParse(json)
if (res.isOk()) {
circuit = res.value
} else {
// eslint-disable-next-line no-console
console.error(res.error.message)
// eslint-disable-next-line no-console
console.error(json)
return
}

this.circuitTitle = (circuit.title || '').trim()

let keepStep = false
Expand Down Expand Up @@ -1570,9 +1587,13 @@ export class QuantumCircuitElement extends HoverableMixin(HTMLElement) {
return this.steps.map(each => each.serialize())
}

// 現在の URL をパースし、最後の / 以降をデコードしたものを返す
private get urlJson(): string {
const json = window.location.href.toString().split(window.location.host)[1].slice(1)
return decodeURIComponent(json)
const url = new URL(location.href, window.location.origin)
const path = decodeURIComponent(url.pathname)
const lastSlashIndex = path.lastIndexOf('/')

return path.substring(lastSlashIndex + 1)
}

clear(): void {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "esnext",
"target": "es2017",
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node"
},
"exclude": ["node_modules", "dist", "*.d.ts"]
Expand Down
53 changes: 44 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2110,18 +2110,34 @@
resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.2.tgz#75d277b030bc11b3be38c807e10071f45ebc78d9"
integrity sha512-ZmiaE3wglXVWBM9fyVC17aGPkLo/UgaOjEiI2FXQfyczrCefORPxIe+2dVmnmk3zkVIbizjrlQzmPGhSYGXG5g==

"@types/eslint-utils@^3.0.0":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@types/eslint-utils/-/eslint-utils-3.0.2.tgz#d5a9eeef2b7c62f396c9034b232386afb5f1abc7"
integrity sha512-NgxYBOYjO5+s+vAKTWgWyBMITF3sxNOmfbRtr3yXA4CZpmfwnOBbqxYO4TGITdhOOf4fSEETEmGpqcoGxdZzjg==
dependencies:
"@types/eslint" "*"
"@types/estree" "*"

"@types/eslint@*":
version "8.44.1"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.44.1.tgz#d1811559bb6bcd1a76009e3f7883034b78a0415e"
integrity sha512-XpNDc4Z5Tb4x+SW1MriMVeIsMoONHCkWFMkR/aPJbzEsxqHy+4Glu/BqTdPrApfDeMaXbtNh6bseNgl5KaWrSg==
dependencies:
"@types/estree" "*"
"@types/json-schema" "*"

"@types/eslint@~8.21.0":
version "8.21.0"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.21.0.tgz#21724cfe12b96696feafab05829695d4d7bd7c48"
integrity sha512-35EhHNOXgxnUgh4XCJsGhE7zdlDhYDN/aMG6UbkByCFFNgQ7b3U+uVoqBpicFydR8JEfgdjCF7SJ7MiJfzuiTA==
version "8.21.3"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.21.3.tgz#5794b3911f0f19e34e3a272c49cbdf48d6f543f2"
integrity sha512-fa7GkppZVEByMWGbTtE5MbmXWJTVbrjjaS8K6uQj+XtuuUv1fsuPAxhygfqLmsb/Ufb3CV8deFCpiMfAgi00Sw==
dependencies:
"@types/estree" "*"
"@types/json-schema" "*"

"@types/estree@*":
version "0.0.50"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw==
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.1.tgz#aa22750962f3bf0e79d753d3cc067f010c95f194"
integrity sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==

"@types/[email protected]":
version "0.0.39"
Expand Down Expand Up @@ -2205,7 +2221,12 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/json-schema@*", "@types/json-schema@^7.0.9":
"@types/json-schema@*":
version "7.0.12"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"
integrity sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==

"@types/json-schema@^7.0.9":
version "7.0.11"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
Expand Down Expand Up @@ -4907,6 +4928,15 @@ eslint-plugin-jsx-a11y@^6.6.0:
object.fromentries "^2.0.6"
semver "^6.3.0"

eslint-plugin-neverthrow@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/eslint-plugin-neverthrow/-/eslint-plugin-neverthrow-1.1.4.tgz#d2f217acafaa092526b9a90f3e15afbe62d4ff4e"
integrity sha512-+8zsE5rDqsDfKYAOq0Fr2jbuxHXTmntIWWJqJA3ms1GAKcVCjl0ycetzOu/hTxot9ctr+WYQpCBgB3F2HATR7A==
dependencies:
"@types/eslint-utils" "^3.0.0"
eslint-utils "3.0.0"
tsutils "3.21.0"

eslint-plugin-no-only-tests@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-no-only-tests/-/eslint-plugin-no-only-tests-3.1.0.tgz#f38e4935c6c6c4842bf158b64aaa20c366fe171b"
Expand Down Expand Up @@ -4940,7 +4970,7 @@ eslint-scope@^7.1.1:
esrecurse "^4.3.0"
estraverse "^5.2.0"

eslint-utils@^3.0.0:
eslint-utils@3.0.0, eslint-utils@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-3.0.0.tgz#8aebaface7345bb33559db0a1f13a1d2d48c3672"
integrity sha512-uuQC43IGctw68pJA1RgbQS8/NP7rch6Cwd4j3ZBtgo4/8Flj4eGE7ZYSZRN3iq5pVUv6GPdW5Z1RFleo84uLDA==
Expand Down Expand Up @@ -7574,6 +7604,11 @@ neo-async@^2.6.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

neverthrow@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/neverthrow/-/neverthrow-6.0.0.tgz#bacd7661cade296ccc5c35760bb3b679214155b6"
integrity sha512-kPZKRs4VkdloCGQXPoP84q4sT/1Z+lYM61AXyV8wWa2hnuo5KpPBF2S3crSFnMrOgUISmEBP8Vo/ngGZX60NhA==

no-case@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/no-case/-/no-case-3.0.4.tgz#d361fd5c9800f558551a8369fc0dcd4662b6124d"
Expand Down Expand Up @@ -10540,7 +10575,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/tsscmp/-/tsscmp-1.0.6.tgz#85b99583ac3589ec4bfef825b5000aa911d605eb"
integrity sha512-LxhtAkPDTkVCMQjt2h6eBVY28KCjikZqZfMcC15YBeNjkgUpdCfBu5HoiOTDu86v6smE8yOjyEktJ8hlbANHQA==

tsutils@^3.21.0:
tsutils@3.21.0, tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
integrity sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==
Expand Down

0 comments on commit a2acdd0

Please sign in to comment.