Skip to content

Commit b986691

Browse files
committed
simplify error message
1 parent 16cb665 commit b986691

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

src/lib/errors.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isSeamHttpApiError } from "@seamapi/http/connect"
2+
3+
export function getErrorMessage(error?: Error | null | undefined): string {
4+
if (isSeamHttpApiError(error)) {
5+
return error.message
6+
}
7+
8+
return t.anUnknownErrorOccurred
9+
}
10+
11+
const t = {
12+
anUnknownErrorOccurred: 'An unknown error occurred',
13+
}

src/lib/ui/thermostat/ClimatePreset.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { SeamHttpApiError } from '@seamapi/http/connect'
21
import classNames from 'classnames'
32
import {
43
type HTMLAttributes,
@@ -9,6 +8,7 @@ import {
98
} from 'react'
109
import { Controller, useForm, type UseFormReturn } from 'react-hook-form'
1110

11+
import { getErrorMessage } from 'lib/errors.js'
1212
import type {
1313
FanModeSetting,
1414
HvacModeSetting,
@@ -71,21 +71,6 @@ interface PresetFormProps {
7171
withKeyField?: boolean
7272
}
7373

74-
function useErrorMessage(
75-
isError: boolean,
76-
error: SeamHttpApiError | null
77-
): string {
78-
return useMemo(() => {
79-
if (!isError) return ''
80-
81-
if (error?.message != null) {
82-
return error.message
83-
}
84-
85-
return t.unknownErrorOccured
86-
}, [error, isError])
87-
}
88-
8974
function PresetForm(props: PresetFormProps): JSX.Element {
9075
const {
9176
defaultValues,
@@ -286,7 +271,7 @@ function CreateForm({ device, onComplete }: CreateFormProps): JSX.Element {
286271
const { mutate, isError, error, isPending } =
287272
useCreateThermostatClimatePreset()
288273

289-
const errorMessage = useErrorMessage(isError, error)
274+
const errorMessage = getErrorMessage(error);
290275

291276
const onSubmit = useCallback(
292277
(values: PresetFormProps['defaultValues']) => {
@@ -381,7 +366,7 @@ function UpdateForm({
381366
[device, mutate, onComplete]
382367
)
383368

384-
const errorMessage = useErrorMessage(isError, error)
369+
const errorMessage = getErrorMessage(error);
385370

386371
return (
387372
<>

src/lib/ui/thermostat/ClimatePresets.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import classNames from 'classnames'
2-
import { type HTMLAttributes, type ReactNode, useMemo, useState } from 'react'
2+
import { type HTMLAttributes, type ReactNode, useState } from 'react'
33

4+
import { getErrorMessage } from 'lib/errors.js'
45
import { AddIcon } from 'lib/icons/Add.js'
56
import { EditIcon } from 'lib/icons/Edit.js'
67
import { FanIcon } from 'lib/icons/Fan.js'
@@ -45,15 +46,7 @@ export function ClimatePresets(props: ClimatePresetsManagement): JSX.Element {
4546
const { mutate, isError, error, isPending } =
4647
useDeleteThermostatClimatePreset()
4748

48-
const errorMessage = useMemo(() => {
49-
if (!isError) return ''
50-
51-
if (error?.message != null) {
52-
return error.message
53-
}
54-
55-
return t.unknownErrorOccured
56-
}, [error, isError])
49+
const errorMessage = getErrorMessage(error);
5750

5851
if (
5952
selectedClimatePreset != null ||
@@ -254,6 +247,5 @@ const t = {
254247
createNew: 'Create New',
255248
delete: 'Delete',
256249
edit: 'Edit',
257-
unknownErrorOccured: 'An unknown error occurred.',
258250
climatePresetNotFound: 'Climate Preset not found.',
259251
}

0 commit comments

Comments
 (0)