1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-16 02:48:52 -05:00
denoland-deno/js/errors.ts
Ryan Dahl 0d03fafbfe Map promises onto futures.
Refactors handlers.rs

The idea is that all Deno "ops" (aka bindings) should map onto
a Rust Future. By setting the "sync" flag in the Base message
users can determine if the future is executed immediately or put
on the event loop.

In the case of async futures, a promise is automatically created.
Errors are automatically forwarded and raised.

TODO:

- The file system ops in src/handler.rs are not using the thread pool
  yet. This will be done in the future using tokio_threadpool::blocking.
  That is, if you try to call them asynchronously, you will get a promise
  and it will act asynchronous, but currently it will be blocking.
- Handlers in src/handler.rs returned boxed futures. This was to make
  it easy while developing. We should try to remove this allocation.
2018-09-09 18:47:22 -04:00

26 lines
620 B
TypeScript

import { deno as fbs } from "gen/msg_generated";
// @internal
export class DenoError<T extends fbs.ErrorKind> extends Error {
constructor(readonly kind: T, msg: string) {
super(msg);
this.name = `deno.${fbs.ErrorKind[kind]}`;
}
}
// @internal
export function maybeThrowError(base: fbs.Base): void {
const err = maybeError(base);
if (err != null) {
throw err;
}
}
export function maybeError(base: fbs.Base): null | DenoError<fbs.ErrorKind> {
const kind = base.errorKind();
if (kind === fbs.ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
}
}