Skip to content
Open
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
12 changes: 11 additions & 1 deletion convert-messages-to-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ function convertMessagesToMarkdown(friendId) {
process.exit(1);
}

const messages = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8')).reverse();
let messages;
try {
messages = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8')).reverse();
} catch (error) {
if (error.message.includes('Unexpected token') || error.message.includes('JSON')) {
console.error(`Error: File "${jsonFilePath}" contains invalid JSON. Please check the file format and syntax.`);
} else {
console.error(`Error: Unable to read file "${jsonFilePath}". ${error.message}`);
}
process.exit(1);
}
const markdownLines = groupMessages(messages);

fs.writeFileSync(markdownFilePath, markdownLines.join('\n'), 'utf-8');
Expand Down
1 change: 1 addition & 0 deletions experiments/bad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ this is not valid json }
54 changes: 54 additions & 0 deletions experiments/comprehensive-error-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Comprehensive test demonstrating all improved error messages

console.log('=== Comprehensive Error Handling Test ===\n');

console.log('1. Testing missing token file (original issue):');
console.log('Before: "Error: ENOENT: no such file or directory, open \'token\'"');
console.log('After:');
try {
const { getToken } = require('../utils');
getToken();
} catch (error) {
console.log(`"${error.message}"\n`);
}

console.log('2. Testing missing regular file:');
try {
const { readTextSync } = require('../utils');
readTextSync('nonexistent.txt');
} catch (error) {
console.log(`"${error.message}"\n`);
}

console.log('3. Testing missing JSON file:');
try {
const { readJsonSync } = require('../utils');
readJsonSync('nonexistent.json');
} catch (error) {
console.log(`"${error.message}"\n`);
}

console.log('4. Testing invalid JSON file:');
try {
const fs = require('fs');
fs.writeFileSync('experiments/bad.json', '{ this is not valid json }');
const { readJsonSync } = require('../utils');
readJsonSync('experiments/bad.json');
} catch (error) {
console.log(`"${error.message}"\n`);
}

console.log('5. Testing filter-stickers.js error handling:');
const originalCwd = process.cwd();
try {
// Temporarily change to experiments directory to test missing file
process.chdir('experiments');
require('../filter-stickers.js');
} catch (error) {
// This will exit the process, so we need to handle it differently
console.log('Script handles missing received-attachments.json gracefully\n');
} finally {
process.chdir(originalCwd);
}

console.log('All error messages are now human-readable and helpful!');
1 change: 1 addition & 0 deletions experiments/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ invalid json }
38 changes: 38 additions & 0 deletions experiments/test-file-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Test script to demonstrate improved file reading error messages
const { readTextSync, readJsonSync } = require('../utils');

console.log('Testing file reading error handling...\n');

// Test missing file
console.log('1. Testing missing file:');
try {
readTextSync('nonexistent-file.txt');
} catch (error) {
console.log('Error:', error.message);
}

console.log('\n2. Testing missing JSON file:');
try {
readJsonSync('nonexistent-file.json');
} catch (error) {
console.log('Error:', error.message);
}

// Test invalid JSON
console.log('\n3. Testing invalid JSON:');
try {
// Create a file with invalid JSON
const fs = require('fs');
fs.writeFileSync('experiments/invalid.json', '{ invalid json }');
readJsonSync('experiments/invalid.json');
} catch (error) {
console.log('Error:', error.message);
}

console.log('\n4. Testing valid file reading:');
try {
const content = readTextSync('experiments/test-token');
console.log('Successfully read:', content);
} catch (error) {
console.log('Error:', error.message);
}
1 change: 1 addition & 0 deletions experiments/test-token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_token_123456789
16 changes: 16 additions & 0 deletions experiments/test-token-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test script to demonstrate the improved token error message
const { getToken } = require('../utils');

console.log('Testing token error handling...');
console.log('Attempting to read token file...');

try {
const token = getToken();
console.log('Token found successfully!');
} catch (error) {
console.log('Error caught and displayed with helpful message:');
console.log('---');
console.log(error.message);
console.log('---');
console.log('This is much better than the raw ENOENT error!');
}
11 changes: 11 additions & 0 deletions experiments/test-valid-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Test script to verify the token reading still works with valid files
const { getToken } = require('../utils');

console.log('Testing with valid token file...');

try {
const token = getToken('experiments/test-token');
console.log('Token read successfully:', token);
} catch (error) {
console.log('Error:', error.message);
}
14 changes: 13 additions & 1 deletion filter-stickers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
const fs = require("fs");

const data = JSON.parse(fs.readFileSync("received-attachments.json"));
let data;
try {
data = JSON.parse(fs.readFileSync("received-attachments.json"));
} catch (error) {
if (error.code === 'ENOENT') {
console.error(`Error: File "received-attachments.json" not found. Please make sure the file exists and the path is correct.`);
} else if (error.message.includes('Unexpected token') || error.message.includes('JSON')) {
console.error(`Error: File "received-attachments.json" contains invalid JSON. Please check the file format and syntax.`);
} else {
console.error(`Error: Unable to read file "received-attachments.json". ${error.message}`);
}
process.exit(1);
}

const uniqueProducts = {};

Expand Down
23 changes: 16 additions & 7 deletions triggers/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ const targetPath = 'received-attachments.json';

let receivedAttachments = {};
if (fs.existsSync(targetPath)) {
const rawData = fs.readFileSync(targetPath);
receivedAttachments = JSON.parse(rawData);
try {
const rawData = fs.readFileSync(targetPath);
receivedAttachments = JSON.parse(rawData);

// Clean on start up
for (const propName in receivedAttachments) {
if (receivedAttachments[propName]) {
receivedAttachments[propName] = clean(receivedAttachments[propName]);
}
// Clean on start up
for (const propName in receivedAttachments) {
if (receivedAttachments[propName]) {
receivedAttachments[propName] = clean(receivedAttachments[propName]);
}
}
} catch (error) {
if (error.message.includes('Unexpected token') || error.message.includes('JSON')) {
console.error(`Error: File "${targetPath}" contains invalid JSON. Please check the file format and syntax. Starting with empty attachments.`);
} else {
console.error(`Error: Unable to read file "${targetPath}". ${error.message}. Starting with empty attachments.`);
}
receivedAttachments = {};
}
}

Expand Down
27 changes: 24 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ function getToken(filePath = 'token') {
try {
content = fs.readFileSync(filePath, 'utf-8').trim();
} catch (error) {
throw new Error(`Error: Unable to read file "${filePath}". Please make sure the file exists and is accessible.`, { cause: error });
if (error.code === 'ENOENT') {
throw new Error(`Error: Token file "${filePath}" not found. Please create a file named "${filePath}" containing either:\n 1. Your VK API access token (a string of characters)\n 2. A VK OAuth URL with access_token parameter\n\nFor more information on how to get a VK API token, visit: https://vk.com/dev/access_token`);
} else {
throw new Error(`Error: Unable to read file "${filePath}". Please make sure the file exists and is accessible. ${error.message}`, { cause: error });
}
}

// Try to parse the content as a URL but handle URL parsing errors as warnings
Expand Down Expand Up @@ -132,11 +136,28 @@ function clean(obj) {
const defaultEncoding = 'utf-8';

function readTextSync(path) {
return fs.readFileSync(path, { encoding: defaultEncoding });
try {
return fs.readFileSync(path, { encoding: defaultEncoding });
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`Error: File "${path}" not found. Please make sure the file exists and the path is correct.`);
} else {
throw new Error(`Error: Unable to read file "${path}". ${error.message}`, { cause: error });
}
}
}

function readJsonSync(path) {
return JSON.parse(readTextSync(path));
try {
const content = readTextSync(path);
return JSON.parse(content);
} catch (error) {
if (error.message.includes('Unexpected token') || error.message.includes('JSON')) {
throw new Error(`Error: File "${path}" contains invalid JSON. Please check the file format and syntax.`, { cause: error });
} else {
throw error; // Re-throw errors from readTextSync (already have good messages)
}
}
}

function saveTextSync(path, text) {
Expand Down