Skip to content

Commit 5a73571

Browse files
committed
Merge branch 'feature/es5-impl' into develop
2 parents 412be22 + dc6b780 commit 5a73571

19 files changed

+1731
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ build/Release
2525
# Dependency directory
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
28+
target/
29+
dist/

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
language: node_js
3+
sudo: false
4+
cache:
5+
directories:
6+
- node_modules
7+
node_js:
8+
- "4.1"
9+
- "4.0"
10+
- "0.12"
11+
- "0.11"
12+
- "0.10"
13+
- "iojs"

LICENSE

+1-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2016 Wave Software
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.
@@ -199,4 +199,3 @@
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201201
limitations under the License.
202-

README.md

+185-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,185 @@
1-
# javascript-eid-exceptions
2-
EID Runtime Exceptions and Utilities for JavaScript
1+
# EID Runtime Exceptions and Utilities for Javascript
2+
3+
[![Build Status](https://travis-ci.org/wavesoftware/javascript-eid-exceptions.svg?branch=master)](https://travis-ci.org/wavesoftware/javascript-eid-exceptions) [![Coverage Status](https://coveralls.io/repos/wavesoftware/javascript-eid-exceptions/badge.svg?branch=master&service=github)](https://coveralls.io/github/wavesoftware/javascript-eid-exceptions?branch=master) [![npm](https://img.shields.io/npm/v/eid.js.svg)](https://www.npmjs.com/package/eid.js) [![Bower](https://img.shields.io/bower/v/eid.js.svg)]()
4+
5+
This small library holds a set of exceptions and utilities that implements idea of fast, reusable, error codes that can be simply thrown fast in case of unpredictable and unrecoverable application failure. It is meant to be used for application bugs.
6+
7+
## Idea
8+
9+
The idea is to use a set of simple runtime exceptions. They should always take the Exception ID (Eid) object in the making. This eid object will then be reported when displaying or logging that exception. It can also be viewed on the professional fatal error window of the application as a bug reference. EidRuntimeExceptions contains also additional unique ID to distinguish each single exception from others with same Eid. This approach simplifies the management of exceptions in the application and allows developers to focus on functionalities rather than coming up with the correct statement for the exception.
10+
11+
This approach is best to use with tools and plugins like:
12+
13+
* [EidGenerator for Netbeans IDE](http://plugins.netbeans.org/plugin/53137/exception-id-eid-generator)
14+
* [Generating Exception Id number in Intellij IDEA with Live Templates](https://github.com/wavesoftware/java-eid-exceptions/wiki/Generating%20Exception%20Id%20number%20in%20Intellij%20IDEA%20with%20Live%20Templates)
15+
16+
Example:
17+
18+
```js
19+
var cause = 'A extra message';
20+
throw new EidIllegalStateException("20150721:100554", cause);
21+
```
22+
23+
Example log:
24+
25+
```
26+
ERROR 2016-01-11T22:48:42.445 EidIllegalStateException: [20150721:100554]<rww5y3> A extra message - Error
27+
at http://localhost:3000/browser/toplevel/eid.min.js:1:3959
28+
at Object.i.3.../eid (http://localhost:3000/browser/toplevel/eid.min.js:1:4211)
29+
at r (http://localhost:3000/browser/toplevel/eid.min.js:1:254)
30+
at http://localhost:3000/browser/toplevel/eid.min.js:1:305
31+
at http://localhost:3000/browser/toplevel/eid.min.js:1:536
32+
at Object.i.1.../../lib/eid (http://localhost:3000/browser/toplevel/eid.min.js:1:974)
33+
at r (http://localhost:3000/browser/toplevel/eid.min.js:1:254)
34+
at t (http://localhost:3000/browser/toplevel/eid.min.js:1:421)
35+
at http://localhost:3000/browser/toplevel/eid.min.js:1:438
36+
```
37+
38+
39+
## Caution
40+
41+
This classes shouldn't be used in any public API or library. It is designed to be used for in-house development of end user applications which will report bugs in standardized error pages or post them to issue tracker.
42+
43+
## NPM
44+
45+
```bash
46+
npm install eid.js --save
47+
```
48+
49+
## Bower
50+
51+
```bash
52+
bower install eid.js --save
53+
```
54+
55+
### `EidPreconditions` class
56+
57+
#### General use
58+
59+
`EidPreconditions` class consists static methods that help to use Eid in a method or constructor. This is solely for convenience purposes. Use them to check whether method or constructor was invoked correctly (whether its preconditions have been met). These methods generally accept a `boolean` expression which is expected to be `true` (or in the case of `checkNotNull`, an object reference which is expected to be non-null). When `false` (or `null`) is passed instead, the `EidPreconditions` method throws an unchecked exception, which helps the calling method communicate to its caller that that caller has made a mistake.
60+
61+
Each method accepts a EID string or Eid object, which is designed to ease of use and provide strict ID for given exception usage. This approach speed up development of large application and helps support teams by giving both static and random ID for each possible bug that could occur.
62+
63+
Each example uses static import:
64+
65+
```js
66+
// nodejs
67+
var EidPreconditions = require('eid/preconditions');
68+
69+
// browser using bower using toplevel version
70+
window.EidPreconditions;
71+
```
72+
73+
#### `checkArgument` method
74+
75+
`checkArgument` method should be used to check argument of the method, and validate it in technical terms (not business terms).
76+
77+
Example:
78+
79+
```js
80+
// [..]
81+
function sqrt(value) {
82+
checkArgument(value >= 0.0, "20150718:012333");
83+
// if ok, calculate the square root
84+
}
85+
```
86+
87+
In this example, `checkArgument` throws an `EidIllegalArgumentException` to indicate that developer made an error in its call to `sqrt`.
88+
89+
#### `checkState` method
90+
91+
`checkState` method should be used to check state of the class in given moment, and validate it in technical terms (not business terms).
92+
93+
Example:
94+
95+
```js
96+
checkState(a >= 3.14 && b < 0., "20150721:115016");
97+
```
98+
99+
#### `checkNotNull` method
100+
101+
`checkNotNull` method should be used to check if given non null argument is actually `null`
102+
103+
Example:
104+
105+
```js
106+
var nonNullUserName = checkNotNull(userName, "20150721:115515");
107+
```
108+
109+
#### `checkElementIndex` method
110+
111+
`checkElementIndex` method can be used to test parameters of an array, before being used
112+
113+
```js
114+
checkElementIndex(index, list.length, "20150721:115749");
115+
```
116+
117+
#### Formatted message support
118+
119+
There have been added additional `message` to method descriptors for `checkArgument`, `checkState`, `checkNotNull` and `checkElementIndex` method. Those method's parameter can sometimes be used to pass additional information to exceptions that will be displayed in log files.
120+
121+
For example:
122+
123+
```js
124+
checkState(transation.isValid(), "20151119:120238", "Invalid transaction: " + transaction);
125+
```
126+
127+
Will produce output similar to;
128+
129+
```
130+
EidIllegalStateException: [20151119:120238]<xf4j1l> => Invalid transaction: <Transaction id=null, buyer=null, products=[]>
131+
```
132+
133+
#### Functional try to execute blocks
134+
135+
You can use functional blocks to handle operations, that are intended to operate properly. This approach simplify the code and makes it more readable. It's also good way to deal with untested, uncovered `catch` blocks. It's easy and gives developers nice way of dealing with countless operations that suppose to work as intended.
136+
137+
Example:
138+
139+
```js
140+
var content = EidPreconditions.tryToExecute(function() {
141+
var fs = require('fs');
142+
return fs.readFileSync('project.properties');
143+
}, "20150718:121521");
144+
```
145+
146+
#### Logging
147+
148+
Eid object can also be useful in logging. That are `makeLogMessage` method provided to do that. Message formatting is done using equivalent of Java's `String.format(String, Object[])` method.
149+
For example:
150+
151+
```js
152+
log.debug(new Eid("20151119:121814").makeLogMessage("REST request received: %s", request));
153+
```
154+
155+
will unfold to something similar to:
156+
157+
```
158+
2017-01-08T16:45:34,334 DEBUG [20151119:121814]<d1afca> REST request received: <RestRequest user=<User id=345> flow=ShowLastTransactions step=Confirm>
159+
```
160+
161+
###Contributing
162+
163+
Contributions are welcome!
164+
165+
To contribute, follow the standard [git flow](http://danielkummer.github.io/git-flow-cheatsheet/) of:
166+
167+
1. Fork it
168+
1. Create your feature branch (`git checkout -b feature/my-new-feature`)
169+
1. Commit your changes (`git commit -am 'Add some feature'`)
170+
1. Push to the branch (`git push origin feature/my-new-feature`)
171+
1. Create new Pull Request
172+
173+
Even if you can't contribute code, if you have an idea for an improvement please open an [issue](https://github.com/wavesoftware/javascript-eid-exceptions/issues).
174+
175+
## Requirements
176+
177+
* NodeJS >= 0.10
178+
* Any modern browser, supported by browserify.
179+
180+
181+
### Releases
182+
183+
- 1.0.0
184+
- initial release
185+
- library ported from Java version

gulp/build.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var gulp = require('gulp');
5+
var browserify = require('browserify');
6+
var source = require('vinyl-source-stream');
7+
var buffer = require('vinyl-buffer');
8+
var uglify = require('gulp-uglify');
9+
var sourcemaps = require('gulp-sourcemaps');
10+
var gutil = require('gulp-util');
11+
var config = require('./config');
12+
13+
var toplevel = 'gulp/build/toplevel.js';
14+
15+
module.exports = {
16+
browserify: function() {
17+
// set up the browserify instance on a task basis
18+
var b = browserify({
19+
entries: toplevel,
20+
debug: true
21+
});
22+
23+
return b.bundle()
24+
.pipe(source('eid.js'))
25+
.pipe(buffer())
26+
.on('error', gutil.log)
27+
.pipe(gulp.dest(config.dist + '/browser/toplevel'));
28+
},
29+
minified: function() {
30+
// set up the browserify instance on a task basis
31+
var b = browserify({
32+
entries: toplevel,
33+
debug: true
34+
});
35+
36+
return b.bundle()
37+
.pipe(source('eid.min.js'))
38+
.pipe(buffer())
39+
.pipe(sourcemaps.init({loadMaps: true}))
40+
// Add transformation tasks to the pipeline here.
41+
.pipe(uglify())
42+
.on('error', gutil.log)
43+
.pipe(sourcemaps.write('./'))
44+
.pipe(gulp.dest(config.dist + '/browser/toplevel'));
45+
}
46+
};

gulp/build/toplevel.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2016 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
(function(wnd) {
18+
'use strict';
19+
20+
wnd.Eid = require('../../lib/eid');
21+
wnd.EidNullPointerException = require('../../lib/eid/exceptions').EidNullPointerException;
22+
wnd.EidIllegalArgumentException = require('../../lib/eid/exceptions').EidIllegalArgumentException;
23+
wnd.EidIllegalStateException = require('../../lib/eid/exceptions').EidIllegalStateException;
24+
wnd.EidIndexOutOfBoundsException = require('../../lib/eid/exceptions').EidIndexOutOfBoundsException;
25+
wnd.EidRuntimeException = require('../../lib/eid/exceptions').EidRuntimeException;
26+
wnd.EidPreconditions = require('../../lib/eid/preconditions');
27+
})(window);

gulp/clean.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var fs = require('fs');
5+
var config = require('./config');
6+
7+
var deleteFolderRecursive = function(path) {
8+
var files = [];
9+
if( fs.existsSync(path) ) {
10+
files = fs.readdirSync(path);
11+
files.forEach(function(file,index){
12+
var curPath = path + "/" + file;
13+
if(fs.lstatSync(curPath).isDirectory()) { // recurse
14+
deleteFolderRecursive(curPath);
15+
} else { // delete file
16+
fs.unlinkSync(curPath);
17+
}
18+
});
19+
fs.rmdirSync(path);
20+
}
21+
};
22+
23+
module.exports = function() {
24+
deleteFolderRecursive(config.target);
25+
deleteFolderRecursive(config.dist);
26+
};

gulp/config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var directory = {
5+
lib: 'lib',
6+
test: 'test',
7+
target: 'target',
8+
dist: 'dist'
9+
};
10+
11+
module.exports = {
12+
sources: [ directory.lib + '/**/*.js' ],
13+
testSources: [ directory.test + '/**/*.js' ],
14+
lib: directory.lib,
15+
test: directory.test,
16+
target: directory.target,
17+
dist: directory.dist,
18+
ceverageMin: 98
19+
};

gulp/serve.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable no-var, strict, prefer-arrow-callback */
2+
'use strict';
3+
4+
var serve = require('gulp-serve');
5+
var config = require('./config');
6+
7+
module.exports = serve([ config.test, config.dist ]);

0 commit comments

Comments
 (0)