Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
arshad-yaseen committed Nov 5, 2024
1 parent 529389c commit ff6b308
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 96 deletions.
82 changes: 0 additions & 82 deletions scripts/install.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
/**
* Base class for Ocra errors.
*/
export class OcraError extends Error {
constructor(message: string) {
super(message);
this.name = 'OcraError';
}
}

/**
* Error thrown when an invalid provider is used.
*/
export class InvalidProviderError extends OcraError {
constructor(provider: string) {
super(`Invalid provider: ${provider}`);
this.name = 'InvalidProviderError';
}
}

/**
* Error thrown when an invalid input is provided.
*/
export class InvalidInputError extends OcraError {
constructor(message: string) {
super(message);
Expand Down
37 changes: 37 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const RED = '\x1b[91m';
const YELLOW = '\x1b[93m';
const RESET = '\x1b[0m';
const BOLD = '\x1b[1m';

export const report = (error: unknown): {message: string; stack?: string} => {
let errorMessage: string;
let errorStack: string | undefined;

if (error instanceof Error) {
errorMessage = error.message;
errorStack = error.stack;
} else if (typeof error === 'string') {
errorMessage = error;
} else {
errorMessage = 'An unknown error occurred';
}

const formattedError = `${RED}${BOLD}[OCRA ERROR] ${errorMessage}${RESET}`;
if (errorStack) {
console.error(
`${formattedError}\n${RED}Stack trace:${RESET}\n${errorStack}`,
);
} else {
console.error(formattedError);
}

return {message: errorMessage, stack: errorStack};
};

export const warn = (message: string): void => {
console.warn(`${YELLOW}${BOLD}[OCRA WARN] ${message}${RESET}`);
};

export const log = (message: string): void => {
console.log(`${BOLD}[OCRA] ${message}${RESET}`);
};
11 changes: 6 additions & 5 deletions src/processors/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {fromPath} from 'pdf2pic';

import {ConcurrencyLimit} from '../classes/concurrency-limit';
import {MAX_CONCURRENT_REQUESTS} from '../constants';
import {report} from '../logger';
import {InputSource, PageResult} from '../types';
import {processImage} from './image';

Expand Down Expand Up @@ -69,7 +70,7 @@ export async function processPdf(
pageNum,
};
} catch (error) {
console.error(`Error converting page ${pageNum}:`, error);
report(error);
throw error;
}
}),
Expand All @@ -90,13 +91,13 @@ export async function processPdf(
pageNumber: pageNum,
},
};
} catch (error: any) {
console.error(`Error processing page ${pageNum}:`, error);
} catch (error: unknown) {
report(error);
return {
page: pageNum,
content: '',
metadata: {
error: error.message || String(error),
error: error instanceof Error ? error.message : String(error),
pageNumber: pageNum,
},
};
Expand All @@ -111,7 +112,7 @@ export async function processPdf(
try {
await fs.rm(tempDir, {recursive: true, force: true});
} catch (error) {
console.error('Error cleaning up temporary directory:', error);
report(error);
}
}
}

0 comments on commit ff6b308

Please sign in to comment.