ExportAllRows.errors.ts93 lines · main
1const normalizeCauseAsError = (cause: unknown): Error | undefined => {
2 if (!cause) return undefined
3
4 if (cause instanceof Error) {
5 return cause
6 }
7
8 if (typeof cause === 'object' && 'message' in cause) {
9 return new Error(String(cause.message))
10 }
11
12 return new Error(String(cause))
13}
14
15export class ExportAllRowsErrorFamily extends Error {
16 cause?: Error
17
18 constructor(message: string, options: { cause?: unknown } = {}) {
19 super(message, options)
20 }
21}
22
23export class NoConnectionStringError extends ExportAllRowsErrorFamily {
24 constructor() {
25 super('No connection string provided for database connection.')
26 this.name = 'NoConnectionStringError'
27 }
28}
29
30export class TableDetailsFetchError extends ExportAllRowsErrorFamily {
31 constructor(tableName: string, _cause?: unknown) {
32 const cause = normalizeCauseAsError(_cause)
33 super(`Failed to fetch table details from the database for table ${tableName}.`, { cause })
34 this.name = 'TableDetailsFetchError'
35 }
36}
37
38export class NoTableError extends ExportAllRowsErrorFamily {
39 constructor(tableName: string) {
40 super(`The specified table "${tableName}" does not exist in the database.`)
41 this.name = 'NoTableError'
42 }
43}
44
45export class NoRowsToExportError extends ExportAllRowsErrorFamily {
46 constructor(tableName: string) {
47 super(`There are no rows to export from the table "${tableName}".`)
48 this.name = 'NoRowsToExportError'
49 }
50}
51
52export class TableTooLargeError extends ExportAllRowsErrorFamily {
53 constructor(tableName: string, rowCount: number, maxAllowed: number) {
54 super(
55 `The table "${tableName}" has ${rowCount} rows, which exceeds the maximum allowed limit of ${maxAllowed} rows for export.`
56 )
57 this.name = 'TableTooLargeError'
58 }
59}
60
61export class FetchRowsError extends ExportAllRowsErrorFamily {
62 constructor(tableName: string, _cause?: unknown) {
63 const cause = normalizeCauseAsError(_cause)
64 super(`An error occurred while fetching rows from the table "${tableName}".`, { cause })
65 this.name = 'FetchRowsError'
66 }
67}
68
69export class OutputConversionError extends ExportAllRowsErrorFamily {
70 constructor(_cause?: unknown) {
71 const cause = normalizeCauseAsError(_cause)
72 super('Failed to convert the fetched rows into the desired output format.', {
73 cause,
74 })
75 this.name = 'OutputConversionError'
76 }
77}
78
79export class BlobCreationError extends ExportAllRowsErrorFamily {
80 constructor(_cause?: unknown) {
81 const cause = normalizeCauseAsError(_cause)
82 super('An error occurred while creating a Blob for the exported data.', { cause })
83 this.name = 'BlobCreationError'
84 }
85}
86
87export class DownloadSaveError extends ExportAllRowsErrorFamily {
88 constructor(_cause?: unknown) {
89 const cause = normalizeCauseAsError(_cause)
90 super('An error occurred while saving the exported data to a file.', { cause })
91 this.name = 'DownloadSaveError'
92 }
93}