berget 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ import chalk from 'chalk';
2
+
3
+ /**
4
+ * Formats and prints error messages in a consistent way
5
+ */
6
+ export function handleError(message: string, error: any): void {
7
+ console.error(chalk.red(`Error: ${message}`));
8
+
9
+ // If the error is a string (like JSON.stringify(error))
10
+ if (typeof error === 'string') {
11
+ try {
12
+ // Try to parse it as JSON
13
+ const parsedError = JSON.parse(error);
14
+ if (parsedError.error) {
15
+ console.error(chalk.dim(`Details: ${parsedError.error}`));
16
+ if (parsedError.code) {
17
+ console.error(chalk.dim(`Code: ${parsedError.code}`));
18
+ }
19
+ } else {
20
+ console.error(chalk.dim(`Details: ${error}`));
21
+ }
22
+ } catch {
23
+ // If it's not valid JSON, just print the string
24
+ console.error(chalk.dim(`Details: ${error}`));
25
+ }
26
+ } else if (error && error.message) {
27
+ // If it's an Error object
28
+ console.error(chalk.dim(`Details: ${error.message}`));
29
+ }
30
+
31
+ // Check for authentication errors
32
+ if (
33
+ (typeof error === 'string' && error.includes('Unauthorized')) ||
34
+ (error && error.message && error.message.includes('Unauthorized')) ||
35
+ (error && error.code && error.code === 401)
36
+ ) {
37
+ console.error(chalk.yellow('\nYou need to be logged in to use this command.'));
38
+ console.error(chalk.yellow('Run `berget login` to authenticate.'));
39
+ }
40
+ }