Compare commits

..

No commits in common. "635d2aea06e6548b80b679411fa128135327602c" and "b56dcd06391814f7eca4d6fc8ce51d80c80d40d7" have entirely different histories.

4 changed files with 5 additions and 85 deletions

View file

@ -1,21 +1,17 @@
# Grip # Grip
[A take on Go Style Error Handling In JavaScript](https://dev.to/nesterow/a-take-on-go-style-error-handling-in-javascript-577)
Simplified result/error handling for JavaScript. Simplified result/error handling for JavaScript.
Grip always returns a consistent call result ready to be handled. Grip always returns a consistent call result ready to be handled.
It makes the control flow similar to that of Golang, but doesn't force you to make additional null checks or create transitional variables to hold error results. It makes the control flow similar to that of Golang, but doesn't force you to make additional null checks or create transitional variables to hold error results.
Instead of returning a nullish error, Grip always returns a consistent status object: Instead of returning a nullish error, Grip always returns a consistent status object:
```javascript ```javascript
const [value, status] = grip(callable) const [value, status] = grip(callable)
if (status.of(MySpecificError)) { if (status.Of(MySpecificError)) {
// handle specific error // handle specific error
} }
if (status.fail()) { if (status.Fail()) {
// handle any error // handle any error
} }
``` ```
@ -24,10 +20,10 @@ The call result is better than tuple:
```javascript ```javascript
const result = grip(callable) const result = grip(callable)
if (result.fail()) { if (result.Fail()) {
// handle any error // handle any error
} }
if (result.of(MySpecificError)) { if (result.Of(MySpecificError)) {
// handle specific error // handle specific error
} }

30
dist/grip.js vendored
View file

@ -18,21 +18,12 @@ class Err extends Error {
Ok() { Ok() {
return false; return false;
} }
ok() {
return this.Ok();
}
Fail() { Fail() {
return true; return true;
} }
fail() {
return this.Fail();
}
Of(cls) { Of(cls) {
return this.cause instanceof cls || this instanceof cls; return this.cause instanceof cls || this instanceof cls;
} }
of(cls) {
return this.Of(cls);
}
static fromCatch(error) { static fromCatch(error) {
const e = new Err(typeof error === "string" ? error : error.message); const e = new Err(typeof error === "string" ? error : error.message);
e.cause = error; e.cause = error;
@ -45,21 +36,12 @@ class Ok {
Ok() { Ok() {
return true; return true;
} }
ok() {
return this.Ok();
}
Fail() { Fail() {
return false; return false;
} }
fail() {
return this.Fail();
}
Of(cls) { Of(cls) {
return this instanceof cls; return this instanceof cls;
} }
of(cls) {
return this.Of(cls);
}
toString() { toString() {
return "Ok"; return "Ok";
} }
@ -82,21 +64,12 @@ class Result extends Array {
Ok() { Ok() {
return this[1].Ok(); return this[1].Ok();
} }
ok() {
return this.Ok();
}
Fail() { Fail() {
return this[1].Fail(); return this[1].Fail();
} }
fail() {
return this.Fail();
}
Of(cls) { Of(cls) {
return this[1].Of(cls); return this[1].Of(cls);
} }
of(cls) {
return this.Of(cls);
}
Iter() { Iter() {
const value = this.value; const value = this.value;
const that = this; const that = this;
@ -119,9 +92,6 @@ class Result extends Array {
} }
}; };
} }
iter() {
return this.Iter();
}
} }
var promise = (result) => { var promise = (result) => {
return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err))); return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err)));

View file

@ -49,7 +49,7 @@ test("fetch err", async () => {
const [result, status] = await grip(fetch("https://localhost:30012")); const [result, status] = await grip(fetch("https://localhost:30012"));
expect(status.Ok()).toBe(false); expect(status.Ok()).toBe(false);
expect(result === null).toBe(true); expect(result === null).toBe(true);
expect(status.of(Error)).toBe(true); expect(status.Of(Error)).toBe(true);
}); });
test("fetch json", async () => { test("fetch json", async () => {

46
grip.ts
View file

@ -2,38 +2,20 @@ interface Status {
message?: string; message?: string;
cause?: any; cause?: any;
Ok(): boolean; Ok(): boolean;
/* alias Ok */
ok(): boolean;
Fail(): boolean; Fail(): boolean;
/* alias Fail */
fail(): boolean;
Of(cls: any): boolean; Of(cls: any): boolean;
/* alias Of */
of(cls: any): boolean;
} }
export class Err extends Error { export class Err extends Error {
Ok() { Ok() {
return false; return false;
} }
/* alias Ok */
ok() {
return this.Ok();
}
Fail() { Fail() {
return true; return true;
} }
/* alias Fail */
fail() {
return this.Fail();
}
Of(cls: any) { Of(cls: any) {
return this.cause instanceof cls || this instanceof cls; return this.cause instanceof cls || this instanceof cls;
} }
/* alias Of */
of(cls: any) {
return this.Of(cls);
}
static fromCatch(error: any) { static fromCatch(error: any) {
const e = new Err(typeof error === "string" ? error : error.message); const e = new Err(typeof error === "string" ? error : error.message);
e.cause = error; e.cause = error;
@ -46,24 +28,12 @@ export class Ok {
Ok() { Ok() {
return true; return true;
} }
/* alias Ok */
ok() {
return this.Ok();
}
Fail() { Fail() {
return false; return false;
} }
/* alias Fail */
fail() {
return this.Fail();
}
Of(cls: any) { Of(cls: any) {
return this instanceof cls; return this instanceof cls;
} }
/* alias Of */
of(cls: any) {
return this.Of(cls);
}
toString() { toString() {
return "Ok"; return "Ok";
} }
@ -96,24 +66,12 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
Ok() { Ok() {
return (this[1] as Status).Ok(); return (this[1] as Status).Ok();
} }
/* alias Ok */
ok() {
return this.Ok();
}
Fail() { Fail() {
return (this[1] as Status).Fail(); return (this[1] as Status).Fail();
} }
/* alias Fail */
fail() {
return this.Fail();
}
Of(cls: any) { Of(cls: any) {
return (this[1] as Status).Of(cls); return (this[1] as Status).Of(cls);
} }
/* alias Of */
of(cls: any) {
return this.Of(cls);
}
Iter() { Iter() {
const value = this.value; const value = this.value;
const that = this; const that = this;
@ -142,10 +100,6 @@ class Result<T> extends Array<T | Status> implements IResult<T> {
}, },
}; };
} }
/* alias Iter */
iter() {
return this.Iter();
}
} }
type Unwrap<T> = type Unwrap<T> =