Skip to content

Commit 960f397

Browse files
authored
Merge pull request #672 from danactive/fix
Standardize API errors
2 parents 93ada6d + 62ad854 commit 960f397

File tree

10 files changed

+359
-417
lines changed

10 files changed

+359
-417
lines changed

package-lock.json

Lines changed: 317 additions & 359 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@emotion/react": "^11.14.0",
22-
"@emotion/styled": "^11.14.0",
22+
"@emotion/styled": "^11.14.1",
2323
"@hello-pangea/dnd": "^18.0.1",
2424
"@mui/joy": "^5.0.0-beta.52",
2525
"camelcase": "^6.3.0",
@@ -42,26 +42,26 @@
4242
"devDependencies": {
4343
"@eslint/js": "^9.28.0",
4444
"@next/eslint-plugin-next": "^15.3.3",
45-
"@playwright/test": "^1.53.1",
45+
"@playwright/test": "^1.53.2",
4646
"@testing-library/jest-dom": "^6.6.3",
4747
"@testing-library/react": "^16.3.0",
4848
"@types/boom": "^7.3.5",
4949
"@types/geojson": "^7946.0.16",
5050
"@types/heic-convert": "^2.1.0",
5151
"@types/jest": "^30.0.0",
5252
"@types/mime-types": "^3.0.1",
53-
"@types/node": "^22.15.33",
53+
"@types/node": "^22.16.0",
5454
"@types/react": "19.1.8",
5555
"@types/react-dom": "19.1.6",
5656
"@types/react-image-gallery": "^1.2.4",
5757
"@types/xml2js": "^0.4.14",
58-
"@typescript-eslint/eslint-plugin": "^8.35.0",
59-
"@typescript-eslint/parser": "^8.35.0",
60-
"eslint": "^9.29.0",
58+
"@typescript-eslint/eslint-plugin": "^8.35.1",
59+
"@typescript-eslint/parser": "^8.35.1",
60+
"eslint": "^9.30.1",
6161
"eslint-config-next": "^15.3.4",
6262
"eslint-plugin-import": "^2.32.0",
6363
"eslint-plugin-jest-dom": "^5.5.0",
64-
"eslint-plugin-jsdoc": "^51.2.3",
64+
"eslint-plugin-jsdoc": "^51.3.2",
6565
"eslint-plugin-jsx-a11y": "^6.10.2",
6666
"eslint-plugin-testing-library": "^7.5.3",
6767
"jest": "^30.0.3",

src/components/AdminAlbum/AdminAlbumClient.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function AdminAlbumClient(
8080
</Select>
8181
</Stack>
8282
{error && <div>{JSON.stringify(error)}</div>}
83-
{!error && album ? <Thumbs album={data?.album} setItem={setItem} /> : <div>Select an album</div>}
83+
{!error && data && data.album ? <Thumbs album={data.album} setItem={setItem} /> : <div>Select an album</div>}
8484
</form>
8585
)
8686
}

src/lib/album.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import transformAlbumSchema, { errorSchema, type ErrorOptionalMessage } from '..
22
import type { Album, AlbumMeta } from '../types/common'
33
import getGalleries from './galleries'
44
import getPersons from './persons'
5+
import { handleLibraryError } from './utils'
56
import { readAlbum } from './xml'
67

78
type Envelope = { body: Album, status: number }
@@ -53,15 +54,9 @@ async function get(
5354
}
5455

5556
return body
56-
} catch (e) {
57+
} catch (err) {
5758
const message = `No album was found; gallery=${gallery}; album=${album};`
58-
if (returnEnvelope) {
59-
return { body: errorSchema(message), status: 404 }
60-
}
61-
62-
63-
console.error('ERROR', message, e)
64-
throw e
59+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
6560
}
6661
}
6762

src/lib/albums.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
XmlGalleryAlbum,
88
} from '../types/common'
99
import getGalleries from './galleries'
10-
import utilsFactory, { isValidStringArray } from './utils'
10+
import utilsFactory, { handleLibraryError, isValidStringArray } from './utils'
1111
import { readGallery } from './xml'
1212

1313
type ErrorOptionalMessage = { albums: object[]; error?: { message: string } }
@@ -111,15 +111,9 @@ async function get(galleryOrGalleries: AlbumMeta['gallery'] | AlbumMeta['gallery
111111
}
112112

113113
return fullOut
114-
} catch (e) {
114+
} catch (err) {
115115
const message = `No albums was found; gallery=${galleryOrGalleries};`
116-
if (returnEnvelope) {
117-
return { body: errorSchema(message), status: 404 }
118-
}
119-
120-
121-
console.error('ERROR', message, e)
122-
throw e
116+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
123117
}
124118
}
125119

src/lib/filesystems.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { glob } from 'glob'
22
import path from 'node:path'
33

4-
import utilsFactory from './utils'
4+
import utilsFactory, { handleLibraryError } from './utils'
55
import transform, { type Filesystem } from '../models/filesystems'
66

77
type ResponseBody = {
@@ -70,12 +70,9 @@ async function get(
7070

7171
const body = { files: sortedFiles, destinationPath }
7272
return (returnEnvelope ? { body, status: 200 } : body)
73-
} catch (e) {
74-
if (returnEnvelope) {
75-
return { body: errorSchema('No files or folders are found'), status: 404 }
76-
}
77-
78-
throw e
73+
} catch (err) {
74+
const message = 'No files or folders are found'
75+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
7976
}
8077
}
8178

src/lib/galleries.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Dirent } from 'node:fs'
22
import fs from 'node:fs/promises'
33

44
import { type Gallery } from '../types/common'
5+
import { handleLibraryError } from './utils'
56

67
type ErrorOptionalMessage = { galleries: object[]; error?: { message: string } }
78
const errorSchema = (message?: string) => {
@@ -43,12 +44,9 @@ async function get(returnEnvelope = false): Promise<
4344
}
4445

4546
return body
46-
} catch (e) {
47-
if (returnEnvelope) {
48-
return { body: errorSchema('No galleries are found'), status: 404 }
49-
}
50-
51-
return errorSchema()
47+
} catch (err) {
48+
const message = 'No galleries were found'
49+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
5250
}
5351
}
5452

src/lib/heifs.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import convert from 'heic-convert'
22
import { readFile, writeFile } from 'node:fs/promises'
33
import { basename } from 'node:path'
44
import type { Filesystem } from './filesystems'
5-
import utilsFactory, { isStandardError } from './utils'
5+
import utilsFactory, { handleLibraryError, isStandardError } from './utils'
66

77
type ResponseBody = {
88
created: string[];
@@ -86,16 +86,9 @@ async function post(
8686
}
8787

8888
return body
89-
} catch (e) {
90-
if (returnEnvelope && isStandardError(e)) {
91-
return { body: errorSchema(e.message), status: 400 }
92-
}
93-
94-
if (returnEnvelope) {
95-
return { body: errorSchema('Fail to process HEIFs'), status: 400 }
96-
}
97-
98-
throw e
89+
} catch (err) {
90+
const message = 'No HEIF files were found'
91+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
9992
}
10093
}
10194

src/lib/persons.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import transformJsonSchema, { errorSchema, type ErrorOptionalMessage } from '../models/person'
22
import type { Gallery, Person } from '../types/common'
3+
import { handleLibraryError } from './utils'
34
import { readPersons } from './xml'
45

56
type Envelope = { body: Person[], status: number }
@@ -33,15 +34,9 @@ async function get(
3334
}
3435

3536
return body
36-
} catch (e) {
37+
} catch (err) {
3738
const message = `No person file was found; gallery=${gallery};`
38-
if (returnEnvelope) {
39-
return { body: errorSchema(message), status: 404 }
40-
}
41-
42-
43-
console.error('ERROR', message, e)
44-
throw e
39+
return handleLibraryError(err, message, returnEnvelope, errorSchema)
4540
}
4641
}
4742

src/lib/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ function simplifyZodMessages(error: z.core.$ZodError) {
101101
}, '')
102102
}
103103

104+
function handleLibraryError(err: unknown, message: string, returnEnvelope: boolean, errorSchema: any) {
105+
if (returnEnvelope) {
106+
if (isStandardError(err)) {
107+
return { body: errorSchema(err.message), status: 500 }
108+
}
109+
return { body: errorSchema(message), status: 404 }
110+
}
111+
112+
console.error('ERROR', message, err)
113+
throw err
114+
}
115+
104116
function isValidStringArray<T extends string = string>(arr: unknown): arr is T[] {
105117
return (
106118
Array.isArray(arr) &&
@@ -197,5 +209,5 @@ function utils() {
197209
}
198210

199211
export default utils
200-
export { isStandardError, isValidStringArray, isZodError, simplifyZodMessages }
212+
export { isStandardError, isValidStringArray, isZodError, simplifyZodMessages, handleLibraryError }
201213

0 commit comments

Comments
 (0)