Skip to content

Commit

Permalink
* now will not show error when it's null
Browse files Browse the repository at this point in the history
* now will split the array of `errorInfo` into `<p>` tags
@ `<PlaceholderError>`

* replace field `response` to its `.body.text()` @ `api/index.ts.FetchResponseError`
* rename class name from camelCase to kebab-case @ `<SelectUser>` & `<PlaceholderError>`
@ fe
  • Loading branch information
n0099 committed Feb 24, 2024
1 parent 5edbc0e commit 25d8550
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
11 changes: 7 additions & 4 deletions fe/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class ApiResponseError extends Error {
}
}
export class FetchResponseError extends Error {
public constructor(public readonly response: Response) {
super(JSON.stringify(response));
public constructor(public readonly bodyText: string) {
super(bodyText);
}
}
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
Expand All @@ -39,11 +39,14 @@ export const queryFunction = <TResponse, TQueryParam>(endpoint: string, queryPar
+ `${_.isEmpty(queryParam) ? '' : '?'}${stringify(queryParam)}`,
{ headers: { Accept: 'application/json' }, signal: queryContext.signal }
);

// must before any Response.text|json() to prevent `Failed to execute 'clone' on 'Response': Response body is already used`
const response2 = response.clone();
const json = await response.json() as TResponse;
if (!response.ok)
throw new FetchResponseError(response);
if (isApiError(json))
throw new ApiResponseError(json.errorCode, json.errorInfo);
if (!response.ok)
throw new FetchResponseError(await response2.text());

return json;
} finally {
Expand Down
22 changes: 11 additions & 11 deletions fe/src/components/placeholders/PlaceholderError.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<template>
<div class="text-center">
<template v-if="error === null">
<span class="errorCode text-muted">error</span>
</template>
<template v-else-if="error instanceof FetchResponseError">
<p class="errorCode text-muted">{{ error.response }}</p>
<template v-if="error instanceof FetchResponseError">
<p class="error-code text-muted">{{ error.bodyText }}</p>
</template>
<template v-else-if="error instanceof ApiResponseError">
<p class="errorCode text-muted">{{ error.errorCode }}</p>
<p class="error-code text-muted">{{ error.errorCode }}</p>
<template v-if="_.isString(error.errorInfo)">
<p v-for="(info, _k) in error.errorInfo.split('\n')" :key="_k">{{ info }}</p>
</template>
<template v-else-if="_.isObject(error.errorInfo)">
<p v-for="(lines, paramName) in error.errorInfo" :key="paramName">
<p v-for="(paramError, paramName) in error.errorInfo" :key="paramName">
参数 {{ paramName }}:
<template v-if="_.isString(lines)">
<p v-for="(info, _k) in lines.split('\n')" :key="_k">{{ info }}</p>
<template v-if="_.isString(paramError)">
<p v-for="(line, _k) in paramError.split('\n')" :key="_k">{{ line }}</p>
</template>
<template v-else-if="_.isArray(paramError) && paramError.length > 1">
<p v-for="(item, _k) in paramError" :key="_k">{{ item }}</p>
</template>
<template v-else>{{ lines }}</template>
<template v-else>{{ paramError }}</template>
</p>
</template>
</template>
Expand All @@ -33,7 +33,7 @@ defineProps<{ error: ApiErrorClass | null }>();
</script>

<style scoped>
.errorCode {
.error-code {
font-size: 6rem;
}
</style>
8 changes: 4 additions & 4 deletions fe/src/components/widgets/SelectUser.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="col-5">
<div class="input-group">
<select v-model="selectBy" class="selectUserBy form-select">
<select v-model="selectBy" class="select-user-by form-select">
<option value="">未选择</option>
<option value="uid">百度UID</option>
<option value="name">用户名</option>
Expand All @@ -10,7 +10,7 @@
<option value="displayNameNULL">NULL覆盖名</option>
</select>
<template v-if="selectBy === 'uid'">
<select v-model="params.uidCompareBy" class="uidCompareBy form-select">
<select v-model="params.uidCompareBy" class="uid-compare-by form-select">
<option>&lt;</option>
<option>=</option>
<option>&gt;</option>
Expand Down Expand Up @@ -107,10 +107,10 @@ onMounted(() => {
</script>

<style scoped>
.selectUserBy {
.select-user-by {
flex-grow: .3;
}
.uidCompareBy {
.uid-compare-by {
flex-grow: .1;
}
</style>

0 comments on commit 25d8550

Please sign in to comment.