We talk about JavaScript. Each month in Warsaw, Poland.
debugging-js by Piotr Zientara @ WarsawJS #13
Słuchałem i pomyślałem, że można lepiej poradzić sobie z błędami...
Orginalny pomysł - kolega z COI: Karol Lassak
Wielkość kodu powoduje wykładniczy wzrost złożoności
Złożoność powoduje liniowy przyrost błędów
// Obiekt przechowujący informacje o błędzie
var eid = new Eid('20160111:173211', 'WJS-001');
// '20160111:173211' - unikalne ID
// 'WJS-001' - opcjonalny numer referencyjny
// z zewnętrznego źródła
var eid = new Eid('20160111:173211');
eid.toString();
↵ [20160111:173211]<sk9ym6>
new Eid('20160111:173211').toString();
↵ [20160111:173211]<b9qoq0>
new Eid('20160111:173211', 'WJS-002').toString();
↵ [20160111:173211|WJS-002]<dbo1az>
var eid = new Eid("20160110:095947");
throw new EidIllegalArgumentException(eid);
throw new EidNullPointerException("20160111:130412");
var cause = 'No react library';
throw new EidIllegalStateException("20150721:100554",
↵ cause);
ERROR 2016-01-11T22:48:42.445 EidIllegalStateException: [20150721:100554]<rww5y3> No react library
at http://localhost:3000/browser/toplevel/eid.min.js:1:3959
at Object.i.3.../eid (http://localhost:3000/browser/toplevel/eid.min.js:1:4211)
at r (http://localhost:3000/browser/toplevel/eid.min.js:1:254)
at http://localhost:3000/browser/toplevel/eid.min.js:1:305
at http://localhost:3000/browser/toplevel/eid.min.js:1:536
at Object.i.1.../../lib/eid (http://localhost:3000/browser/toplevel/eid.min.js:1:974)
at r (http://localhost:3000/browser/toplevel/eid.min.js:1:254)
at t (http://localhost:3000/browser/toplevel/eid.min.js:1:421)
at http://localhost:3000/browser/toplevel/eid.min.js:1:438);
// podzielona linia na fragmenty
EidIllegalStateException: ↵ // typ błędu, zawsze
[20150721:100554] ↵ // ID unikalne, zawsze
<rww5y3> ↵ // losowe ID, zawsze
No react library // komunikat, opcjonalny
Aby pracować wydajnie i uniknąć copy & paste, potrzebne nam generatory...
W najbliższych planach wsparcie dla IDE Atom
Przykład importu:
var checkNotNull = Eid.preconditions.checkNotNull;
checkArgument method should be used to check argument of the method, and validate it in technical terms (not business terms)
function sqrt(value) {
checkArgument(value >= 0.0, "20150718:012333");
// if ok, calculate the square root
}
checkState method should be used to check state of the class in given moment, and validate it in technical terms (not business terms)
checkState(r >= 0.0 && mass > 0.0, "20150721:115016");
checkNotNull method should be used to check, if given not null argument is actually null
var safeUser = checkNotNull(user, "20150721:115515");
// także checkNotUndefined
// oraz checkNotNullable
checkElementIndex method can be used to test parameters of an array, before being used
checkElementIndex(index, list.length, "20150721:115749");
tryToExecute method can be used to handle operations, that are intended to operate properly, but technically can fail
var content = EidPreconditions.tryToExecute(function() {
var fs = require('fs');
return fs.readFileSync('bower.json');
}, "20150718:121521");
log.debug(new Eid("20151119:121814")
↵ .makeLogMessage("REST request: %s", request));
2017-01-08 16:45:34,334 DEBUG [20151119:121814]<d1afca>
↵ REST request received: <RestRequest user=<User id=345>
↵ flow=ShowLastTransactions step=Confirm>