Skip to content

Commit

Permalink
error handling on async
Browse files Browse the repository at this point in the history
  • Loading branch information
nonylene committed Dec 31, 2023
1 parent daf3498 commit 8e55719
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
57 changes: 31 additions & 26 deletions app/routes/search._pukiwiki-base.pukiwiki/els-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { Page, PageResult } from "./models";
import { toQueryString } from "~/utils";

interface PukiWikiSearch {
query?: string;
order: string;
page: number;
useRawQuery: boolean;
}

export const SEARCH_SIZE = 35;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -30,28 +23,39 @@ export function createPageResultFromResponse(json: any): PageResult {
};
}

// (SearchQuery) => promise
export async function requestSearch(search: PukiWikiSearch) {
let query: string;
if (search.query == null || search.query == "") {
query = "*";
export function buildPukiWikiSearch(
order: string,
page: number,
useRawQuery: boolean,
query?: string,
) {
let queryString: string;
if (query == null || query == "") {
queryString = "*";
} else {
query = search.useRawQuery ? search.query : toQueryString(search.query);
queryString = useRawQuery ? query : toQueryString(query);
}
return _requestSearch(
query,
SEARCH_SIZE,
(search.page - 1) * SEARCH_SIZE,
search.order,
);
return {
query: queryString,
size: SEARCH_SIZE,
from: (page - 1) * SEARCH_SIZE,
order: order,
};
}

async function _requestSearch(
query: string,
size: number,
from: number,
order: string,
) {
interface PukiWikiSearch {
query: string;
size: number;
from: number;
order: string;
}

export async function requestSearch({
query,
size,
from,
order,
}: PukiWikiSearch) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const queryJson: any = {
query: {
Expand Down Expand Up @@ -134,8 +138,9 @@ async function _requestSearch(

const response = await fetch(url);
if (!response.ok) {
throw new Error(
throw new Response(
`Invalid response from the backend elasticsearch server: ${response.statusText}`,
{ status: 500 },
);
}
const json = await response.json();
Expand Down
11 changes: 4 additions & 7 deletions app/routes/search._pukiwiki-base.pukiwiki/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Elasticsearch 起因のエラーを表示する Component をわけるために route を分けている
import { SEARCH_SIZE, requestSearch } from "./els-client";
import { SEARCH_SIZE, buildPukiWikiSearch, requestSearch } from "./els-client";
import { PageResult } from "./models";
import { PageList, links as pageListLinks } from "./page-list";
import { LinksFunction, LoaderFunctionArgs, defer } from "@remix-run/node";
Expand All @@ -23,12 +23,9 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
const searchParams = new URL(request.url).searchParams;
const { query, page, order, advanced } = parseSearchParams(searchParams);

const pageResult = requestSearch({
query,
order,
page,
useRawQuery: advanced,
});
// async 内で throw Response するとうまくいかないのでパースは non-async でやる
const search = buildPukiWikiSearch(order, page, advanced, query);
const pageResult = requestSearch(search);

const pukiwikiBaseURL = process.env.HEINEKEN_PUKIWIKI_BASE_URL!;

Expand Down
23 changes: 16 additions & 7 deletions app/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export function toQueryString(query: string) {
currentWord += char;
break;
case "quoteend":
throw Error(
throw new Response(
"Query parse error: 単語と単語の間はスペースを入れてください",
{ status: 400 },
);
}
break;
Expand All @@ -84,8 +85,9 @@ export function toQueryString(query: string) {
positive = false;
break;
case "quoteend":
throw Error(
throw new Response(
"Query parse error: 単語と単語の間はスペースを入れてください",
{ status: 400 },
);
}
break;
Expand All @@ -101,8 +103,9 @@ export function toQueryString(query: string) {
break;
case "noword":
if (!positive) {
throw Error(
throw new Response(
"Query parse error: '-' は単語の直前か単語内にのみ配置できます",
{ status: 400 },
);
}
break;
Expand All @@ -119,15 +122,17 @@ export function toQueryString(query: string) {
state = "quoteend";
break;
case "unquoted":
throw Error(
throw new Response(
"Query parse error: '\"' を単語内で用いる場合は '\\' でエスケープしてください",
{ status: 400 },
);
case "noword":
state = "quoted";
break;
case "quoteend":
throw Error(
throw new Response(
"Query parse error: 単語と単語の間はスペースを入れてください",
{ status: 400 },
);
}
break;
Expand All @@ -137,7 +142,9 @@ export function toQueryString(query: string) {
// End
switch (state) {
case "quoted":
throw Error("Query parse error: '\"' の対応が合いません");
throw new Response("Query parse error: '\"' の対応が合いません", {
status: 400,
});
case "unquoted":
endWord();
break;
Expand All @@ -148,7 +155,9 @@ export function toQueryString(query: string) {

for (const [word] of words) {
if (word.length < 2) {
throw Error("Query error: 各単語の長さは 2 以上にしてください");
throw new Response("Query error: 各単語の長さは 2 以上にしてください", {
status: 400,
});
}
}

Expand Down

0 comments on commit 8e55719

Please sign in to comment.