Skip to content

Commit

Permalink
chore: add error code enum, so we can include in docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MP70 committed Jun 13, 2024
1 parent 0ec6c07 commit 432ad9b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 54 deletions.
57 changes: 20 additions & 37 deletions src/drawPDFTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,20 @@ import {
// endMarkedContent
} from "./helpers/markContent";
import { generateColumnWidths } from "./helpers/columnWidths";
import validateAndConvertTableData from "./helpers/validateAndConvert"; // Adjust the path as needed
import validateAndConvertTableData from "./helpers/validateAndConvert";

import { drawElement } from "./helpers/drawContent";
import {
CellContent,
DrawTableOptions,
ErrorCode,
TableDimensions,
TableObject,
TableOptionsDeepPartial,
} from "../types";
import { calcTableHeight, calcRowHeight } from "./helpers/tableHeights";
import { setDefaults } from "./helpers/setDefaults";
export class DrawTableError extends Error {
code: string;
dimensions?: Partial<TableDimensions>;
rowHeights?: number[];

constructor(
code: string,
message: string,
dimensions?: Partial<TableDimensions>,
rowHeights?: number[]
) {
super(message);
this.code = code;
this.name = "DrawTableError";
this.dimensions = dimensions;
this.rowHeights = rowHeights;
}
}

import { createTableError } from "./helpers/errorFactory";
/**
* Draws a table on the provided PDF document page.
*
Expand Down Expand Up @@ -116,9 +99,9 @@ export async function drawTable(
fillEmpty: fillUndefCells,
});
} catch (error: any) {
throw new DrawTableError(
"ERR_CONVERT_VALIDATE",
`Error validating or converting table data:${error.message}`
throw createTableError(
ErrorCode.ERR_CONVERT_VALIDATE,
`Error validating or converting table data: ${error.message}`,
);
}

Expand All @@ -127,9 +110,9 @@ export async function drawTable(
column.overrideWidths.length > 0 &&
column.overrideWidths.length !== tableData[0].length
) {
throw new DrawTableError(
"ERR_COLUMN_COUNT_MISMATCH",
"The number of columns in overrideWidths does not match the number of columns in the table."
throw createTableError(
ErrorCode.ERR_COLUMN_COUNT_MISMATCH,
"The number of columns in overrideWidths does not match the number of columns in the table.",
);
}

Expand All @@ -138,9 +121,9 @@ export async function drawTable(
row?.overrideHeights?.length > 0 &&
row.overrideHeights?.length !== tableData.length
) {
throw new DrawTableError(
"ERR_ROW_COUNT_MISMATCH",
"The number of rows in overrideHeights does not match the number of rows in the table."
throw createTableError(
ErrorCode.ERR_ROW_COUNT_MISMATCH,
"The number of rows in overrideHeights does not match the number of rows in the table.",
);
}

Expand Down Expand Up @@ -175,8 +158,8 @@ export async function drawTable(
);
// Check for table width overflow
if (tableWidth > availableWidth) {
throw new DrawTableError(
"ERR_TABLE_WIDTH_OVERFLOW",
throw createTableError(
ErrorCode.ERR_TABLE_WIDTH_OVERFLOW,
"Table width exceeds the available space on the page.",
{ width: tableWidth, endX: startX + tableWidth }
);
Expand All @@ -196,13 +179,13 @@ export async function drawTable(
border.width,
title.text,
title.textSize,
row.overrideHeights // This is the new parameter
row.overrideHeights
);

// Check for table height overflow
if (tableHeightDetails.totalHeight > availableHeight) {
throw new DrawTableError(
"ERR_TABLE_HEIGHT_OVERFLOW",
throw createTableError(
ErrorCode.ERR_TABLE_HEIGHT_OVERFLOW,
"Table height exceeds the available space on the page.",
{
width: tableWidth,
Expand Down Expand Up @@ -404,9 +387,9 @@ export async function drawTable(

currentY -= rowHeight;
} catch (error: any) {
throw new DrawTableError(
"DRAW_ROW_ERROR",
`Failed to draw at ROW-${rowIndex}: ${error.message}`
throw createTableError(
ErrorCode.DRAW_ROW_ERROR,
`Failed to draw at ROW-${rowIndex}: ${error.message}`,
);
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/helpers/columnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
GenColumnWidthOptions,
CustomStyledText,
Link,
ErrorCode,
} from "../../types";
import { PDFFont } from "pdf-lib";
import { Image, CellElement } from "../../types";
import { isImage, isLink } from "./util";
import { DrawTableError } from "../drawPDFTable";
import { createTableError } from "./errorFactory";

/**
* Get the width of the content based on its type.
Expand Down Expand Up @@ -110,9 +111,9 @@ export function generateColumnWidths(options: GenColumnWidthOptions): number[] {
}
if (columnWidthMode === "wrapHeader") {
if (!hasHeader) {
throw new DrawTableError(
"ERR_WRAP_HEADER_INVALID",
`Failed to draw, wrap header not valid when no header set `,
throw createTableError(
ErrorCode.ERR_WRAP_HEADER_INVALID,
"Failed to draw, wrap header not valid when no header set",
);
}
return headerWidths;
Expand Down Expand Up @@ -166,9 +167,9 @@ export function generateColumnWidths(options: GenColumnWidthOptions): number[] {
if (headerTotalWidth >= availableWidth) {
const scaleFactor = adjustedWidth / headerTotalWidth;
if (scaleFactor < 0.9) {
throw new DrawTableError(
"ERR_NO_SPACE_FOR_HEADERS",
`Drawing this would require us to squish the headers too much (<90%). Please choose equal, give us more page width, or manually set col widths.`,
throw createTableError(
ErrorCode.ERR_NO_SPACE_FOR_HEADERS,
"Drawing this would require us to squish the headers too much (<90%). Please choose equal, give us more page width, or manually set col widths.",
);
}
columnWidths = columnWidths.map((width, index) =>
Expand Down Expand Up @@ -202,8 +203,8 @@ export function generateColumnWidths(options: GenColumnWidthOptions): number[] {
return columnWidths;
}
} else {
throw new DrawTableError(
"ERR_INVALID_DISTRIBUTE_MODE",
throw createTableError(
ErrorCode.ERR_INVALID_DISTRIBUTE_MODE,
'Invalid distribute mode. Choose "auto", "wrapHeader" or "equal".',
);
}
Expand Down
29 changes: 29 additions & 0 deletions src/helpers/errorFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ErrorCode, TableDimensions } from "../../types";

export class DrawTableError extends Error {
code: ErrorCode;
dimensions?: Partial<TableDimensions>;
rowHeights?: number[];

constructor(
code: ErrorCode,
message: string,
dimensions?: Partial<TableDimensions>,
rowHeights?: number[]
) {
super(message);
this.code = code;
this.name = "DrawTableError";
this.dimensions = dimensions;
this.rowHeights = rowHeights;
}
}

export function createTableError(
code: ErrorCode,
message: string,
dimensions?: Partial<TableDimensions>,
rowHeights?: number[]
): DrawTableError {
return new DrawTableError(code, message, dimensions, rowHeights);
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { drawTable } from "./drawPDFTable";
import { generateColumnWidths } from "./helpers/columnWidths";
import { calcTableHeight } from "./helpers/tableHeights";
import { DrawTableError } from "./drawPDFTable";
import { DrawTableError } from "./helpers/errorFactory";
import {
GenColumnWidthOptions,
CellContent,
Expand All @@ -23,6 +23,7 @@ import {
TitleOptions,
ImageBase,
LinkBase,
ErrorCode,
} from "../types";

// Export your functions and types
Expand Down Expand Up @@ -50,4 +51,5 @@ export {
ImageBase,
LinkBase,
DrawTableError,
ErrorCode,
};
26 changes: 19 additions & 7 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ import {
// Alignment options for text elements
export type Alignment = "left" | "center" | "right";

//Currently internal only, nor exposed to the user.
//Currently internal only, nor exposed to the user.
//Optimal mode will come in a future release, as it needs a bit more testing. May also add a "none" mode.
export enum BreakWordMode {
Essential = "essential", //DEFAULT ON
Optimal = "optimal",
}

export enum ErrorCode {
ERR_CONVERT_VALIDATE, // Error when data validation or conversion fails
ERR_COLUMN_COUNT_MISMATCH, // Error when the column count does not match expected values
ERR_ROW_COUNT_MISMATCH, // Error when the row count does not match expected values
ERR_TABLE_WIDTH_OVERFLOW, // Error when table width exceeds available space
ERR_TABLE_HEIGHT_OVERFLOW, // Error when table height exceeds available space
DRAW_ROW_ERROR, // Error when a row fails to draw correctly
ERR_WRAP_HEADER_INVALID, // Indicates invalid usage of wrapHeader without a header
ERR_NO_SPACE_FOR_HEADERS, // Indicates insufficient space for headers
ERR_INVALID_DISTRIBUTE_MODE, // Indicates an invalid column width distribution mode
}

// Options for distributing column widths in the table
export type GenColumnWidthOptions = {
columnWidthMode: "equal" | "auto" | "wrapHeader"; // Mode for calculating column widths
Expand Down Expand Up @@ -107,12 +119,12 @@ export type TableOptionsDeepPartial<T> = {
[K in keyof T]?: T[K] extends (...args: any[]) => any
? T[K] | undefined
: T[K] extends Color
? T[K] | undefined
: T[K] extends PDFFont
? T[K] | undefined
: T[K] extends object
? TableOptionsDeepPartial<T[K]>
: T[K];
? T[K] | undefined
: T[K] extends PDFFont
? T[K] | undefined
: T[K] extends object
? TableOptionsDeepPartial<T[K]>
: T[K];
};

// Basic options for the table, including font, colors, and alignment
Expand Down

0 comments on commit 432ad9b

Please sign in to comment.