Skip to content

Commit e047db1

Browse files
committed
Normalize error messages
1 parent 15e6afc commit e047db1

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

bin/handler.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ export async function handleRender(source, options) {
3535
export async function handleParse(source) {
3636
let { content, error } = await read(source);
3737
if (error) {
38-
console.log(error.message);
38+
console.warn(error.message);
3939
process.exit(1);
4040
} else {
4141
try {
4242
console.log(JSON.stringify(parse(content), null, 2));
4343
} catch (e) {
44-
console.log(e.message);
44+
console.error(e.message);
4545
process.exit(1);
4646
}
4747
}
@@ -61,14 +61,14 @@ export async function handlePreview(source, options) {
6161
export async function handleGenerateSVG(source) {
6262
let { content, error } = await read(source);
6363
if (error) {
64-
console.log(error.message);
64+
console.error(error.message);
6565
process.exit(1);
6666
} else {
6767
content = content.trim();
6868
if (/^svg\s*\{/i.test(content) || !content.length) {
6969
console.log(generateSVG(content));
7070
} else {
71-
console.log('Not a valid SVG format');
71+
console.warn('warn: invalid SVG format');
7272
}
7373
}
7474
}
@@ -83,13 +83,17 @@ export async function handleGenerateShape(source) {
8383
}
8484
}
8585

86-
export function handleSetConfig(field, value) {
87-
config[field] = value;
88-
if (value === '') {
89-
delete config[field];
90-
}
91-
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
92-
console.log('OK')
86+
export async function handleSetConfig(field, value) {
87+
config[field] = value;
88+
if (value === '') {
89+
delete config[field];
90+
}
91+
try {
92+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
93+
console.log('ok');
94+
} catch (e) {
95+
console.error('error: failed to write config file');
96+
}
9397
}
9498

9599
export function handleDisplayConfig(field) {
@@ -109,9 +113,9 @@ async function read(path) {
109113
let content = '';
110114
let error = null;
111115
if (path === undefined) {
112-
console.log('No source file specified, reading from stdin...');
116+
console.log('No source file specified, reading from stdin:');
113117
let key = os.platform() === 'win32' ? 'CTRL+Z' : 'CTRL+D';
114-
console.log(`Press ${key} to finish input.\n`);
118+
console.log(`(Press ${key} to finish input.)\n`);
115119
try {
116120
content = await readFromStdin();
117121
} catch (e) {

src/preview/client.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
try {
5353
input = JSON.parse(e.data);
5454
} catch(e) {
55-
console.error(e);
55+
console.error(e.message);
5656
}
5757
let { data, type } = input;
5858
switch (type) {

src/preview/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ wsServer.on('connection', (ws, req) => {
5959
data: read(sourceFile)
6060
}));
6161

62-
ws.on('error', err => {
63-
console.error(err);
62+
ws.on('error', e => {
63+
console.error(e.message);
6464
});
6565

6666
ws.on('message', (e) => {
6767
let input = {};
6868
try {
6969
input = JSON.parse(e);
7070
} catch(e) {
71-
console.error(e);
71+
console.error(e.message);
7272
}
7373
if (input.type === 'ping') {
7474
clearTimeout(timers[query.get('instance')]);

src/static.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function getCssDoodleLib() {
4646
if (fs.existsSync(libPath)) {
4747
return fs.readFileSync(libPath, 'utf8');
4848
} else {
49-
console.warn(`[warn] css-doodle not found: ${libPath}. Use default css-doodle instead.`);
50-
console.info('[info] Please check it with `config` command.\n');
49+
console.warn(`warn: css-doodle not found: ${libPath}. Use default css-doodle instead.`);
50+
console.info('Please check it with `config` command.\n');
5151
}
5252
}
5353
return read('../node_modules/css-doodle/css-doodle.min.js');
@@ -62,8 +62,8 @@ export function getBrowserPath() {
6262
if (fs.existsSync(browserPath)) {
6363
return browserPath;
6464
} else {
65-
console.warn(`[warn] Browser not found: ${browserPath}. Use default browser instead.`);
66-
console.info('[info] Please check it with `config` command.\n');
65+
console.warn(`warn: browser not found: ${browserPath}. Use default browser instead.`);
66+
console.info('Please check it with `config` command.\n');
6767
}
6868
}
6969
return '';

0 commit comments

Comments
 (0)