Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export const ERROR_NO_CONTENT = 'content variable is empty';
export const ERROR_TYPE_NOT_STRING = 'content variable is not a string';

/** Status strings that are not MarkupGeneratorError codes. */
export const HTML_EMAIL_SUCCESS = 'Content has correct html!!!';
export const HTML_EMAIL_ERROR = 'Content has not correct html!!!';

export const CONST_FILE_CREATED = '';
export const CONST_FILE_NOT_WRITTEN = 'file not written';
16 changes: 15 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ export type MarkupGeneratorErrorCode =
| 'EIMPORT'
| 'EREAD';

export const DEFAULT_MESSAGES: Record<MarkupGeneratorErrorCode, string> = {
EMPTY_CONTENT: 'content variable is empty',
NOT_A_STRING: 'content variable is not a string',
FILE_EXISTS: 'file already exists',
WRITE_FAILED: 'file not written',
JSON_READ: 'failed to read JSON file',
JSON_PARSE: 'failed to parse JSON file',
EIMPORT: 'failed to import module',
EREAD: 'failed to read path',
};

export class MarkupGeneratorError extends Error {
readonly code: MarkupGeneratorErrorCode;

constructor(code: MarkupGeneratorErrorCode, message: string) {
constructor(
code: MarkupGeneratorErrorCode,
message: string = DEFAULT_MESSAGES[code]
) {
super(message);
this.name = 'MarkupGeneratorError';
this.code = code;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type {
RenderOptions,
WriteOptions,
} from './templateGenerator';
export { MarkupGeneratorError } from './errors';
export { MarkupGeneratorError, DEFAULT_MESSAGES } from './errors';
export type { MarkupGeneratorErrorCode } from './errors';

export {
Expand Down
14 changes: 8 additions & 6 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { WriteGeneratedFileOptions } from './types';
* - existing files are overwritten unless a caller uses writeGeneratedFile({ overwrite: 'error' })
* - empty content throws EMPTY_CONTENT
* - non-string content throws NOT_A_STRING
* - write failures throw WRITE_FAILED (default message from DEFAULT_MESSAGES)
*/
const writeHTML = async (
fileName: string,
Expand All @@ -23,14 +24,11 @@ const writeHTML = async (
message: string = ''
): Promise<void> => {
if (!data) {
throw new MarkupGeneratorError('EMPTY_CONTENT', 'content variable is empty');
throw new MarkupGeneratorError('EMPTY_CONTENT');
}

if (typeof data !== 'string') {
throw new MarkupGeneratorError(
'NOT_A_STRING',
'content variable is not a string'
);
throw new MarkupGeneratorError('NOT_A_STRING');
}

if (dir === '') {
Expand All @@ -46,7 +44,7 @@ const writeHTML = async (
console.log(message);
}
} catch {
throw new MarkupGeneratorError('WRITE_FAILED', 'file not written');
throw new MarkupGeneratorError('WRITE_FAILED');
}
};

Expand All @@ -63,6 +61,10 @@ const writingFile = async (content: string, name: string = 'prefix'): Promise<vo
await writeHTML(generateFileName(name), content);
};

/**
* Preferred writer. Returns the absolute path that was written.
* Default overwrite policy is replace. Use overwrite: 'error' to throw FILE_EXISTS.
*/
const writeGeneratedFile = async (
options: WriteGeneratedFileOptions
): Promise<string> => {
Expand Down
22 changes: 22 additions & 0 deletions tests/default-messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from 'vitest';
import { DEFAULT_MESSAGES, MarkupGeneratorError } from '../src/index';

describe('DEFAULT_MESSAGES', () => {
test('every code has a default message', () => {
expect(new MarkupGeneratorError('EMPTY_CONTENT').message).toBe(
DEFAULT_MESSAGES.EMPTY_CONTENT
);
expect(new MarkupGeneratorError('WRITE_FAILED').message).toBe(
DEFAULT_MESSAGES.WRITE_FAILED
);
expect(new MarkupGeneratorError('FILE_EXISTS').message).toBe(
DEFAULT_MESSAGES.FILE_EXISTS
);
});

test('explicit message still wins', () => {
expect(new MarkupGeneratorError('FILE_EXISTS', 'custom').message).toBe(
'custom'
);
});
});
6 changes: 3 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
readSourceFile,
writeHTML,
writingFile,
DEFAULT_MESSAGES,
} from '../src/index';
import { ERROR_NO_CONTENT, ERROR_TYPE_NOT_STRING } from '../src/constants';

const UUID_RE = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';

Expand Down Expand Up @@ -50,14 +50,14 @@ describe('writeHTML', () => {

test('throws when content is empty', async () => {
await expect(writeHTML('empty.html', '', outDir)).rejects.toThrow(
ERROR_NO_CONTENT
DEFAULT_MESSAGES.EMPTY_CONTENT
);
});

test('throws when content is not a string', async () => {
await expect(
writeHTML('bad.html', 123 as unknown as string, outDir)
).rejects.toThrow(ERROR_TYPE_NOT_STRING);
).rejects.toThrow(DEFAULT_MESSAGES.NOT_A_STRING);
});
});

Expand Down
Loading