ExportAllRows.errors.ts 2.8 KB

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