-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject error codes from Devtools (#1281)
* Add functionality to inject error and log messages. * move injection code into separate file * remove unused devDependency * fix linter * Update .changeset/gentle-kiwis-admire.md Co-authored-by: Jerel Miller <[email protected]> --------- Co-authored-by: Jerel Miller <[email protected]>
- Loading branch information
1 parent
7d3d445
commit badb1f9
Showing
14 changed files
with
3,239 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"apollo-client-devtools": minor | ||
--- | ||
|
||
Add the ability for devtools to provide full error and log messages without the need to need to call `loadDevMessages` or `loadErrorMessages`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Create PR for new error codes | ||
|
||
on: [workflow_dispatch] | ||
|
||
jobs: | ||
build: | ||
defaults: | ||
run: | ||
working-directory: ./all-clients | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "18.x" | ||
- run: | | ||
npm run rebuild | ||
npm install | ||
npm run encode | ||
if git diff --exit-code --quiet HEAD errorcodes.json; then | ||
echo "No new error codes were added." | ||
exit 0 | ||
fi | ||
cat <<EOF > ../.changeset/$(md5 < errorcodes.json).md | ||
"apollo-client-devtools": patch | ||
--- | ||
add error codes for new Apollo Client version | ||
EOF | ||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@v6 | ||
with: | ||
title: "[chore] add error codes for new Apollo Client version" | ||
commit-message: "[chore] add error codes for new Apollo Client version" | ||
branch: "pr/update-errorcodes" | ||
delete-branch: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
all-clients/errorcodes.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import pkg from "./package.json" with { type: "json" }; | ||
import { writeFile } from "node:fs/promises"; | ||
import { restoreErrorCodes } from "./restore-errorcodes.mjs"; | ||
import assert from "node:assert"; | ||
import { gt } from "semver"; | ||
|
||
function getLookupArray() { | ||
const map = new Map(); | ||
const array = [null]; | ||
function lookup(value) { | ||
if (map.has(value)) { | ||
return map.get(value); | ||
} else { | ||
const index = array.push(value) - 1; | ||
map.set(value, index); | ||
return index; | ||
} | ||
} | ||
return [array, lookup]; | ||
} | ||
|
||
const [allMessages, getMessageIndex] = getLookupArray(); | ||
const [allFiles, getFileIndex] = getLookupArray(); | ||
const [allConditions, getConditionIndex] = getLookupArray(); | ||
const byVersion = {}; | ||
|
||
const prefix = "@apollo-client/"; | ||
for (const partialPath of Object.keys(pkg.dependencies).sort((a, b) => | ||
gt(a.substring(prefix.length), b.substring(prefix.length)) ? 1 : -1 | ||
)) { | ||
const match = new RegExp("/([^/]*)$").exec(partialPath); | ||
if (!match) continue; | ||
const version = match[1]; | ||
const from = import.meta.resolve(`${partialPath}/invariantErrorCodes.js`); | ||
|
||
const { devDebug, devLog, devWarn, devError, errorCodes } = await import( | ||
from | ||
); | ||
const combinedEntries = Object.entries({ | ||
...devDebug, | ||
...devLog, | ||
...devWarn, | ||
...devError, | ||
...errorCodes, | ||
}); | ||
|
||
const collected = new Uint16Array( | ||
(Math.max(...Object.keys(combinedEntries)) + 1) * 3 | ||
); | ||
|
||
for (const entry of combinedEntries) { | ||
const code = entry[0] - 1; | ||
/** @type {{file: string, condition?: string, message: string}} */ | ||
const { file, condition, message } = entry[1]; | ||
|
||
collected[code * 3] = getMessageIndex(message); | ||
collected[code * 3 + 1] = getFileIndex(file); | ||
collected[code * 3 + 2] = getConditionIndex(condition); | ||
} | ||
byVersion[version] = Buffer.from(collected).toString("base64"); | ||
|
||
// we immediately restore to verify a full roundtrip | ||
assert.deepStrictEqual( | ||
restoreErrorCodes( | ||
{ allMessages, allConditions, allFiles, byVersion }, | ||
version | ||
), | ||
Object.fromEntries(combinedEntries) | ||
); | ||
} | ||
const encoded = JSON.stringify( | ||
{ | ||
allMessages, | ||
allConditions, | ||
allFiles, | ||
byVersion, | ||
}, | ||
undefined, | ||
2 | ||
); | ||
|
||
writeFile("errorcodes.json", encoded, "utf-8"); |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.