Skip to content

Commit a864ac0

Browse files
committed
First commit.
0 parents  commit a864ac0

6 files changed

Lines changed: 154 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/.history
3+
*.lock
4+
error_log
5+
/test/*

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/.history
3+
*.lock
4+
error_log
5+
/test/*

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Error Printer for NodeJS
2+
This project provides a simple function for nodejs where you can automatically write the error message, error code, error date and formatted error date to a file.
3+
4+
###### How to test:
5+
```
6+
yarn test
7+
```
8+
9+
###### How to run:
10+
```
11+
yarn start
12+
```
13+
14+
###### How to use in the project:
15+
```
16+
(in ES6)
17+
import error_log from "error-printer";
18+
19+
try {
20+
// ... any codes
21+
} catch(err) {
22+
error_log({
23+
message: err.message
24+
});
25+
}
26+
```

index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const fs = require("fs");
2+
const moment = require("moment");
3+
4+
let MAIN_DIRECTORY = __dirname.replace(/\\/g, '/').replace("/node_modules/error-printer", "");
5+
let OPTIONS = {
6+
logFileName: "error_log",
7+
messageTitle: "Error message",
8+
codeTitle: "Error code",
9+
datePureTitle: "Error Date ( Pure )",
10+
dateFormattedTitle: "Error Date ( Formatted )",
11+
formattedDateConfig: "DD MMMM YYYY hh:mm A",
12+
isWriteCode: true,
13+
isWritePureDate: true,
14+
isWriteFormattedDate: true
15+
};
16+
17+
const setMainDirectory = (directory) => {
18+
MAIN_DIRECTORY = directory;
19+
}
20+
21+
const setOptions = (options) => {
22+
OPTIONS = {
23+
...OPTIONS,
24+
...options
25+
}
26+
};
27+
28+
const error_log = ({
29+
options,
30+
message,
31+
code
32+
}) => {
33+
const _options = {
34+
...OPTIONS,
35+
...options
36+
};
37+
38+
// Error file exists control:
39+
const fileExists = fs.existsSync(MAIN_DIRECTORY + `/${_options.logFileName}`);
40+
41+
// If error file do not exists, it will be create:
42+
if (!fileExists) {
43+
fs.writeFileSync(MAIN_DIRECTORY + `/${_options.logFileName}`, "");
44+
}
45+
46+
// Error message:
47+
let errorData = `
48+
${_options.messageTitle}: ${message}
49+
`;
50+
51+
if (_options.isWriteCode) {
52+
errorData += `${_options.codeTitle}: ${code}`;
53+
}
54+
55+
if (_options.isWritePureDate) {
56+
errorData += `${_options.datePureTitle}: ${new Date().toISOString()}\n`;
57+
}
58+
59+
if (_options.isWriteFormattedDate) {
60+
errorData += `${_options.dateFormattedTitle}: ${moment(new Date()).format(_options.formattedDateConfig)}\n`;
61+
}
62+
63+
errorData += `\n`;
64+
65+
// Write new error message to error_log file.
66+
fs.appendFile(MAIN_DIRECTORY + `/${_options.logFileName}`, errorData, err => {
67+
if (err) {
68+
console.error(err);
69+
return err;
70+
}
71+
});
72+
};
73+
74+
module.exports = error_log;
75+
module.exports.setMainDirectory = setMainDirectory;
76+
module.exports.setOptions = setOptions;

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "error-printer",
3+
"version": "1.0.0-pre-alpha.1",
4+
"description": "This project was developed for Node JS APIs need to error handling on production.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node .",
8+
"test": "node test"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/lfabl/error-printer.git"
13+
},
14+
"keywords": [
15+
"error",
16+
"handler",
17+
"logger"
18+
],
19+
"author": "lfabl",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/lfabl/error-printer/issues"
23+
},
24+
"homepage": "https://github.com/lfabl/error-printer#readme",
25+
"dependencies": {
26+
"moment": "^2.29.3"
27+
}
28+
}

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const error_log = require(".");
2+
const {
3+
setMainDirectory
4+
} = require(".");
5+
6+
setMainDirectory(__dirname + "/test");
7+
8+
error_log({
9+
options: {
10+
logFileName: "test"
11+
},
12+
message: "halo",
13+
code: 400
14+
});

0 commit comments

Comments
 (0)