Skip to content

Commit

Permalink
#779: turn on useUnknownInCatchVariables (#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller authored Dec 5, 2021
1 parent ea0185b commit d9df585
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/background/proxyService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ describe("proxy service requests", () => {
expect(data).toEqual({ foo: 42 });
});

describe.each([[400], [401], [403], [500]])(
describe.each([[400], [401], [403], [405], [500]])(
"remote status: %s",
(statusCode) => {
it("can proxy remote error", async () => {
const reason = "Bad request";

axiosMock.onAny().reply(200, {
json: {},
reason: "Bad request",
reason,
status_code: statusCode,
});

Expand All @@ -194,7 +196,7 @@ describe("proxy service requests", () => {
const { status, statusText } = ((error as ContextError)
.cause as AxiosError).response;
expect(status).toEqual(statusCode);
expect(statusText).toEqual("Bad request");
expect(statusText).toEqual(reason);
}
});
}
Expand Down
11 changes: 7 additions & 4 deletions src/background/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ const backgroundRequest = liftBackground(
async (config: AxiosRequestConfig) => {
try {
return cleanResponse(await axios(config));
} catch (error) {
// Axios offers its own serialization method, but it doesn't include the response.
// By deleting toJSON, the serialize-error library will use its default serialization
} catch (error: unknown) {
if (isAxiosError(error)) {
// Axios offers its own serialization method, but it doesn't include the response.
// By deleting toJSON, the serialize-error library will use its default serialization
delete error.toJSON;
}

console.trace("Error performing request from background page", { error });
delete error.toJSON;
throw error;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/contrib/google/bigquery/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ async function ensureBigQuery(): Promise<void> {
// https://github.com/google/google-api-javascript-client/blob/master/docs/reference.md
try {
await gapi.client.load("bigquery", "v2");
} catch (error) {
} catch (error: unknown) {
console.debug("Error fetching BigQuery API definition", {
error: error.error,
error,
});
throw new Error("Error fetching BigQuery API definition");
}
Expand Down
28 changes: 21 additions & 7 deletions src/contrib/google/sheets/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Effect } from "@/types";
import { Effect, UnknownObject } from "@/types";
import { BlockArg, BlockOptions, Schema } from "@/core";
import { propertiesToSchema } from "@/validators/generic";
import { isNullOrBlank } from "@/utils";
import { unary } from "lodash";
import { isPlainObject, unary } from "lodash";
import { validateRegistryId } from "@/types/helpers";
import { normalizeHeader } from "@/contrib/google/sheets/sheetsHelpers";
import { sheets } from "@/background/messenger/api";
import { getErrorMessage } from "@/errors";

type CellValue = string | number | null;

Expand Down Expand Up @@ -103,8 +104,11 @@ export const GOOGLE_SHEETS_APPEND_ID = validateRegistryId(
"@pixiebrix/google/sheets-append"
);

function isAuthError(error: { code: number }): boolean {
return [404, 401, 403].includes(error.code);
function isAuthError(error: unknown): boolean {
return (
isPlainObject(error) &&
([404, 401, 403] as unknown[]).includes((error as UnknownObject).code)
);
}

export class GoogleSheetsAppend extends Effect {
Expand All @@ -120,7 +124,15 @@ export class GoogleSheetsAppend extends Effect {
inputSchema: Schema = APPEND_SCHEMA;

async effect(
{ spreadsheetId, tabName, rowValues: rawValues = {} }: BlockArg,
{
spreadsheetId,
tabName,
rowValues: rawValues = {},
}: BlockArg<{
spreadsheetId: string;
tabName: string;
rowValues: Record<string, CellValue> | RowValue[];
}>,
{ logger }: BlockOptions
): Promise<void> {
const rowValues =
Expand All @@ -139,8 +151,10 @@ export class GoogleSheetsAppend extends Effect {
console.debug(
`Found headers for ${tabName}: ${currentHeaders.join(", ")}`
);
} catch (error) {
logger.warn(`Error retrieving headers: ${error.toString()}`, error);
} catch (error: unknown) {
logger.warn(`Error retrieving headers: ${getErrorMessage(error)}`, {
error,
});
if (isAuthError(error)) {
throw error;
}
Expand Down
17 changes: 9 additions & 8 deletions src/devTools/Locator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ import { useAsyncEffect } from "use-async-effect";
import { isEmpty } from "lodash";
import { thisTab } from "@/devTools/utils";
import { detectFrameworks, searchWindow } from "@/contentScript/messenger/api";
import { getErrorMessage } from "@/errors";

function useSearchWindow(query: string) {
function useSearchWindow(query: string): [unknown[] | null, unknown | null] {
const { tabId } = browser.devtools.inspectedWindow;
const [results, setResults] = useState([]);
const [error, setError] = useState();
const [results, setResults] = useState<unknown[] | null>([]);
const [error, setError] = useState<unknown | null>();

useAsyncEffect(
async (isMounted) => {
if (!query) return;
setError(undefined);
setResults(undefined);
setError(null);
setResults(null);
try {
const { results } = await searchWindow(thisTab, query);
if (!isMounted()) return;
setResults(results as any);
} catch (error) {
setResults(results);
} catch (error: unknown) {
if (!isMounted()) return;
setError(error);
}
Expand Down Expand Up @@ -92,7 +93,7 @@ const Locator: React.FunctionComponent = () => {
/>
</InputGroup>

{searchError?.toString()}
{searchError && getErrorMessage(searchError)}

{searchResults == null ? (
<GridLoader />
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"resolveJsonModule": true,
"baseUrl": ".",
"skipLibCheck": false,
"useUnknownInCatchVariables": true,

// TODO: Drop these lines to make TS stricter https://github.com/pixiebrix/pixiebrix-extension/issues/775
"strictNullChecks": false,
Expand All @@ -19,7 +20,6 @@
"noImplicitReturns": false,
"noUnusedParameters": false,
"useDefineForClassFields": false,
"useUnknownInCatchVariables": false, // https://github.com/pixiebrix/pixiebrix-extension/issues/779
"paths": {
"@/*": ["src/*"],
"@img/*": ["img/*"],
Expand Down

0 comments on commit d9df585

Please sign in to comment.