Back to home

Error Message Example

Demonstrates two error scenarios: form validation (ErrorMessage) and the full-screen error page (ErrorShower).

ErrorShower full-screen error page

A wavy-animated ERROR display; pass the error text.

404 Not FoundERROR

ErrorMessage form validation

Shows red text on error, a green check when valid and submitted.

Error Message (Error / ErrorMessage) Usage Guide

Overview

Error feedback in the project falls into four scenarios:

ScenarioImplementationNotes
Form validationErrorMessage componentRed text on error, green check when valid
Full-screen error pageErrorShower component (Error.tsx)Wavy-animated ERROR display
API errorFetcher + Result + toastShows a translated toast.error when code != "0"
Error copyErrorMessage message namespaceerror key → localized copy mapping

ErrorMessage — form validation hint

A controlled validation hint, wired to react-hook-form's errors / isSubmitted.

import ErrorMessage from "@/common/components/i18n/ErrorMessage";

<ErrorMessage message={errors.userName?.message} submitted={isSubmitted} />
PropTypeRequiredDescription
messagestringNoError copy; shows red text when set
submittedbooleanYesWhether submitted; shows a green check when valid and submitted
messageClassstringNoError text class, default text-red-600
rightClassstringNoGreen check class, default text-red-600

Render logic: has message → red text; no message and submitted → green check; otherwise empty.


ErrorShower — wavy ERROR animation

The full-screen error component exported from Error.tsx; it splits "ERROR" into characters with motion-plus's splitText and animates them in a wave.

import ErrorShower from "@/common/components/Error";

<ErrorShower error="404 Not Found" />
PropTypeRequiredDescription
errorstringYesThe error copy to display

Dependencies: motion + motion-plus (splitText). Styles are injected via an inline <style>.


Result — API response protocol

Fetcher parses every response as a Result; any code other than "0" is treated as a business error.

interface Result {
    code: string;       // "0" means success
    message?: string;   // error copy (used when not translated)
    data: any;          // business payload
    key?: string;       // error key (for i18n)
    instruction?: string;
}

Fetcher logic (simplified):

const result = await response.json() as Result;
if (result.code != "0") {
    const message = translator ? translator(result.key) : result.message;
    toast.error(message);  // react-hot-toast
}

So it first looks up result.key in the ErrorMessage namespace, then falls back to result.message.


ErrorMessage message namespace

The ErrorMessage namespace in messages/default/{locale}.json maps error keys to copy; the server-returned key is passed to translator.

keyEnglish (example)
system_server_errorSystem error, please retry later...
global_parameter_nullParameter must not be empty...
class_not_foundClass not found...

When adding a new error, agree on a key on the server and add matching copy to en.json / zh.json under ErrorMessage.