From 5586bbc898b2c3b7f92aed9b70dc71fc3b589d60 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 1 Sep 2022 14:12:29 -0700 Subject: [PATCH 01/39] framework errors --- packages/@jsii/kernel/src/kernel.ts | 93 +++++++++++++++----------- packages/@jsii/kernel/src/objects.ts | 13 ++-- packages/@jsii/kernel/src/recording.ts | 4 +- packages/@jsii/runtime/lib/host.ts | 12 ++-- 4 files changed, 68 insertions(+), 54 deletions(-) diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 7426dab9da..14018b7d66 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -15,6 +15,13 @@ import * as onExit from './on-exit'; import * as wire from './serialization'; import * as tar from './tar-cache'; +export class JsiiFault extends Error { + public constructor(message: string) { + super(message); + this.name = 'JsiiFault'; + } +} + export class Kernel { /** * Set to true for verbose debugging. @@ -56,7 +63,7 @@ export class Kernel { this._debug('load', req); if ('assembly' in req) { - throw new Error( + throw new JsiiFault( '`assembly` field is deprecated for "load", use `name`, `version` and `tarball` instead', ); } @@ -70,7 +77,7 @@ export class Kernel { // module exists, verify version const epkg = fs.readJsonSync(path.join(packageDir, 'package.json')); if (epkg.version !== pkgver) { - throw new Error( + throw new JsiiFault( `Multiple versions ${pkgver} and ${epkg.version} of the ` + `package '${pkgname}' cannot be loaded together since this is unsupported by ` + 'some runtime environments', @@ -135,7 +142,9 @@ export class Kernel { `loadAssemblyFromPath(${packageDir})`, ); } catch (e: any) { - throw new Error(`Error for package tarball ${req.tarball}: ${e.message}`); + throw new JsiiFault( + `Error for package tarball ${req.tarball}: ${e.message}`, + ); } // load the module and capture its closure @@ -166,13 +175,15 @@ export class Kernel { const epkg = fs.readJsonSync(path.join(packageDir, 'package.json')); if (!epkg.bin) { - throw new Error('There is no bin scripts defined for this package.'); + throw new JsiiFault( + 'There is no bin scripts defined for this package.', + ); } const scriptPath = epkg.bin[req.script]; if (!epkg.bin) { - throw new Error(`Script with name ${req.script} was not defined.`); + throw new JsiiFault(`Script with name ${req.script} was not defined.`); } const result = cp.spawnSync( @@ -198,7 +209,7 @@ export class Kernel { signal: result.signal, }; } - throw new Error(`Package with name ${req.assembly} was not loaded.`); + throw new JsiiFault(`Package with name ${req.assembly} was not loaded.`); } public create(req: api.CreateRequest): api.CreateResponse { @@ -221,7 +232,7 @@ export class Kernel { const ti = this._typeInfoForProperty(property, fqn); if (!ti.static) { - throw new Error(`property ${symbol} is not static`); + throw new JsiiFault(`property ${symbol} is not static`); } const prototype = this._findSymbol(fqn); @@ -244,11 +255,11 @@ export class Kernel { const ti = this._typeInfoForProperty(property, fqn); if (!ti.static) { - throw new Error(`property ${symbol} is not static`); + throw new JsiiFault(`property ${symbol} is not static`); } if (ti.immutable) { - throw new Error(`static property ${symbol} is readonly`); + throw new JsiiFault(`static property ${symbol} is readonly`); } const prototype = this._findSymbol(fqn); @@ -299,7 +310,7 @@ export class Kernel { const propInfo = this._typeInfoForProperty(req.property, fqn, interfaces); if (propInfo.immutable) { - throw new Error( + throw new JsiiFault( `Cannot set value of immutable property ${req.property} to ${req.value}`, ); } @@ -328,7 +339,7 @@ export class Kernel { // verify this is not an async method if (ti.async) { - throw new Error(`${method} is an async method, use "begin" instead`); + throw new JsiiFault(`${method} is an async method, use "begin" instead`); } const fqn = jsiiTypeFqn(obj); @@ -365,12 +376,12 @@ export class Kernel { const ti = this._typeInfoForMethod(method, fqn); if (!ti.static) { - throw new Error(`${fqn}.${method} is not a static method`); + throw new JsiiFault(`${fqn}.${method} is not a static method`); } // verify this is not an async method if (ti.async) { - throw new Error(`${method} is an async method, use "begin" instead`); + throw new JsiiFault(`${method} is an async method, use "begin" instead`); } const prototype = this._findSymbol(fqn); @@ -404,7 +415,7 @@ export class Kernel { this._debug('begin', objref, method, args); if (this.syncInProgress) { - throw new Error( + throw new JsiiFault( `Cannot invoke async method '${req.objref[TOKEN_REF]}.${req.method}' while sync ${this.syncInProgress} is being processed`, ); } @@ -413,7 +424,7 @@ export class Kernel { // verify this is indeed an async method if (!ti.async) { - throw new Error(`Method ${method} is expected to be an async method`); + throw new JsiiFault(`Method ${method} is expected to be an async method`); } const fqn = jsiiTypeFqn(obj); @@ -448,7 +459,7 @@ export class Kernel { const storedPromise = this.promises.get(promiseid); if (storedPromise == null) { - throw new Error(`Cannot find promise with ID: ${promiseid}`); + throw new JsiiFault(`Cannot find promise with ID: ${promiseid}`); } const { promise, method } = storedPromise; @@ -458,7 +469,7 @@ export class Kernel { this._debug('promise result:', result); } catch (e) { this._debug('promise error:', e); - throw e; + throw new JsiiFault((e as any).message); } return { @@ -497,7 +508,7 @@ export class Kernel { const cb = this.waiting.get(cbid); if (!cb) { - throw new Error(`Callback ${cbid} not found`); + throw new JsiiFault(`Callback ${cbid} not found`); } if (err) { @@ -530,7 +541,9 @@ export class Kernel { const assembly = this._assemblyFor(assemblyName); const targets = assembly.metadata.targets; if (!targets) { - throw new Error(`Unexpected - "targets" for ${assemblyName} is missing!`); + throw new JsiiFault( + `Unexpected - "targets" for ${assemblyName} is missing!`, + ); } return { naming: targets }; @@ -581,12 +594,12 @@ export class Kernel { }; case spec.TypeKind.Interface: - throw new Error( + throw new JsiiFault( `Cannot create an object with an FQN of an interface: ${fqn}`, ); default: - throw new Error(`Unexpected FQN kind: ${fqn}`); + throw new JsiiFault(`Unexpected FQN kind: ${fqn}`); } } @@ -635,10 +648,10 @@ export class Kernel { for (const override of overrides) { if (api.isMethodOverride(override)) { if (api.isPropertyOverride(override)) { - throw new Error(overrideTypeErrorMessage); + throw new JsiiFault(overrideTypeErrorMessage); } if (methods.has(override.method)) { - throw new Error( + throw new JsiiFault( `Duplicate override for method '${override.method}'`, ); } @@ -647,10 +660,10 @@ export class Kernel { this._applyMethodOverride(obj, objref, fqn, interfaces, override); } else if (api.isPropertyOverride(override)) { if (api.isMethodOverride(override)) { - throw new Error(overrideTypeErrorMessage); + throw new JsiiFault(overrideTypeErrorMessage); } if (properties.has(override.property)) { - throw Error( + throw new JsiiFault( `Duplicate override for property '${override.property}'`, ); } @@ -658,7 +671,7 @@ export class Kernel { this._applyPropertyOverride(obj, objref, fqn, interfaces, override); } else { - throw new Error(overrideTypeErrorMessage); + throw new JsiiFault(overrideTypeErrorMessage); } } } @@ -679,7 +692,7 @@ export class Kernel { ) { // error if we can find a method with this name if (this._tryTypeInfoForMethod(override.property, typeFqn, interfaces)) { - throw new Error( + throw new JsiiFault( `Trying to override method '${override.property}' as a property`, ); } @@ -802,7 +815,7 @@ export class Kernel { ) { // error if we can find a property with this name if (this._tryTypeInfoForProperty(override.method, typeFqn, interfaces)) { - throw new Error( + throw new JsiiFault( `Trying to override property '${override.method}' as a method`, ); } @@ -940,7 +953,7 @@ export class Kernel { if (!fn) { fn = instance[methodName]; if (!fn) { - throw new Error(`Cannot find ${methodName} on object`); + throw new JsiiFault(`Cannot find ${methodName} on object`); } } return { ti, obj: instance, fn }; @@ -954,7 +967,7 @@ export class Kernel { // error if args > params if (args.length > params.length && !(method && method.variadic)) { - throw new Error( + throw new JsiiFault( `Too many arguments (method accepts ${params.length} parameters, got ${args.length} arguments)`, ); } @@ -969,7 +982,7 @@ export class Kernel { } // No vararg was provided for (let j = i; j < params.length; j++) { if (!param.optional && params[j] === undefined) { - throw new Error( + throw new JsiiFault( `Unexpected 'undefined' value at index ${ j - i } of variadic argument '${ @@ -979,7 +992,7 @@ export class Kernel { } } } else if (!param.optional && arg === undefined) { - throw new Error( + throw new JsiiFault( `Not enough arguments. Missing argument for the required parameter '${ param.name }' of type '${spec.describeTypeReference(param.type)}'`, @@ -991,7 +1004,7 @@ export class Kernel { private _assemblyFor(assemblyName: string) { const assembly = this.assemblies.get(assemblyName); if (!assembly) { - throw new Error(`Could not find assembly: ${assemblyName}`); + throw new JsiiFault(`Could not find assembly: ${assemblyName}`); } return assembly; } @@ -1010,7 +1023,7 @@ export class Kernel { curr = curr[name]; } if (!curr) { - throw new Error(`Could not find symbol ${fqn}`); + throw new JsiiFault(`Could not find symbol ${fqn}`); } return curr; } @@ -1021,13 +1034,13 @@ export class Kernel { const assembly = this.assemblies.get(moduleName); if (!assembly) { - throw new Error(`Module '${moduleName}' not found`); + throw new JsiiFault(`Module '${moduleName}' not found`); } const types = assembly.metadata.types ?? {}; const fqnInfo = types[fqn]; if (!fqnInfo) { - throw new Error(`Type '${fqn}' not found`); + throw new JsiiFault(`Type '${fqn}' not found`); } return fqnInfo; @@ -1044,7 +1057,7 @@ export class Kernel { interfaces && interfaces.length > 0 ? ` or interface(s) ${interfaces.join(', ')}` : ''; - throw new Error( + throw new JsiiFault( `Class ${fqn}${addendum} doesn't have a method '${methodName}'`, ); } @@ -1114,7 +1127,7 @@ export class Kernel { properties = interfaceTypeInfo.properties; bases = interfaceTypeInfo.interfaces ?? []; } else { - throw new Error( + throw new JsiiFault( `Type of kind ${typeInfo.kind} does not have properties`, ); } @@ -1148,7 +1161,7 @@ export class Kernel { interfaces && interfaces.length > 0 ? ` or interface(s) ${interfaces.join(', ')}` : ''; - throw new Error( + throw new JsiiFault( `Type ${fqn}${addendum} doesn't have a property '${property}'`, ); } @@ -1233,7 +1246,7 @@ export class Kernel { parametersCopy.push(parametersCopy[parametersCopy.length - 1]); } if (xs.length > parametersCopy.length) { - throw new Error( + throw new JsiiFault( `Argument list (${JSON.stringify( xs, )}) not same size as expected argument list (length ${ diff --git a/packages/@jsii/kernel/src/objects.ts b/packages/@jsii/kernel/src/objects.ts index 293732ea48..921c4dcf77 100644 --- a/packages/@jsii/kernel/src/objects.ts +++ b/packages/@jsii/kernel/src/objects.ts @@ -2,6 +2,7 @@ import * as spec from '@jsii/spec'; import * as api from './api'; import { EMPTY_OBJECT_FQN } from './serialization'; +import { JsiiFault } from './kernel'; /** * Symbol under which we store the { type -> objid } map on object instances @@ -136,7 +137,7 @@ export class ObjectTable { interfaces?: string[], ): api.ObjRef { if (fqn === undefined) { - throw new Error('FQN cannot be undefined'); + throw new JsiiFault('FQN cannot be undefined'); } const existingRef = objectReference(obj); @@ -179,13 +180,13 @@ export class ObjectTable { */ public findObject(objref: api.ObjRef): RegisteredObject { if (typeof objref !== 'object' || !(api.TOKEN_REF in objref)) { - throw new Error(`Malformed object reference: ${JSON.stringify(objref)}`); + throw new JsiiFault(`Malformed object reference: ${JSON.stringify(objref)}`); } const objid = objref[api.TOKEN_REF]; const obj = this.objects.get(objid); if (!obj) { - throw new Error(`Object ${objid} not found`); + throw new JsiiFault(`Object ${objid} not found`); } // If there are "additional" interfaces declared on the objref, merge them @@ -215,7 +216,7 @@ export class ObjectTable { */ public deleteObject({ [api.TOKEN_REF]: objid }: api.ObjRef) { if (!this.objects.delete(objid)) { - throw new Error(`Object ${objid} not found`); + throw new JsiiFault(`Object ${objid} not found`); } } @@ -267,7 +268,7 @@ class InterfaceCollection implements Iterable { public addFromClass(fqn: string): void { const ti = this.resolveType(fqn); if (!spec.isClassType(ti)) { - throw new Error( + throw new JsiiFault( `Expected a class, but received ${spec.describeTypeReference(ti)}`, ); } @@ -288,7 +289,7 @@ class InterfaceCollection implements Iterable { public addFromInterface(fqn: string): void { const ti = this.resolveType(fqn); if (!spec.isInterfaceType(ti)) { - throw new Error( + throw new JsiiFault( `Expected an interface, but received ${spec.describeTypeReference(ti)}`, ); } diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 0f77519403..6827369276 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -1,6 +1,6 @@ import * as fs from 'fs-extra'; -import { Kernel } from './kernel'; +import { JsiiFault, Kernel } from './kernel'; export async function closeRecording(kernel: Kernel): Promise { const logfile: fs.WriteStream = (kernel as any).logfile; @@ -60,7 +60,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { return ret; } catch (e: any) { logOutput({ error: e.message }); - throw e; + throw new JsiiFault(e.message); } }, }); diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 44a38dec7b..e660271e01 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,4 @@ -import { api, Kernel } from '@jsii/kernel'; +import { api, Kernel, JsiiFault } from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -48,7 +48,7 @@ export class KernelHost { function completeCallback(this: KernelHost): void { const req = this.inout.read(); if (!req || 'exit' in req) { - throw new Error('Interrupted before callback returned'); + throw new JsiiFault('Interrupted before callback returned'); } // if this is a completion for the current callback, then we can @@ -59,7 +59,7 @@ export class KernelHost { completeReq.complete.cbid === callback.cbid ) { if (completeReq.complete.err) { - throw new Error(completeReq.complete.err); + throw new JsiiFault(completeReq.complete.err); } return completeReq.complete.result; @@ -93,13 +93,13 @@ export class KernelHost { */ private processRequest(req: Input, next: () => void, sync = false) { if ('callback' in req) { - throw new Error( + throw new JsiiFault( 'Unexpected `callback` result. This request should have been processed by a callback handler', ); } if (!('api' in req)) { - throw new Error('Malformed request, "api" field is required'); + throw new JsiiFault('Malformed request, "api" field is required'); } const apiReq = req; @@ -161,7 +161,7 @@ export class KernelHost { function checkIfAsyncIsAllowed() { if (sync) { - throw new Error( + throw new JsiiFault( 'Cannot handle async operations while waiting for a sync callback to return', ); } From db512f8e2406395727dd5734160fb6002654252d Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 7 Sep 2022 15:29:05 -0700 Subject: [PATCH 02/39] tmp --- packages/@jsii/kernel/src/api.ts | 3 +++ packages/@jsii/kernel/src/kernel.ts | 12 ++++++++++-- packages/@jsii/kernel/src/objects.ts | 6 ++++-- packages/@jsii/runtime/lib/host.ts | 15 ++++++++------- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/@jsii/kernel/src/api.ts b/packages/@jsii/kernel/src/api.ts index b339a4716b..7b0f0d845b 100644 --- a/packages/@jsii/kernel/src/api.ts +++ b/packages/@jsii/kernel/src/api.ts @@ -1,3 +1,5 @@ +//import { JsiiErrorType } from './kernel'; + export const TOKEN_REF = '$jsii.byref'; export const TOKEN_INTERFACES = '$jsii.interfaces'; export const TOKEN_DATE = '$jsii.date'; @@ -276,5 +278,6 @@ export interface OkayResponse { export interface ErrorResponse { readonly error: string; + //readonly type: JsiiErrorType; readonly stack?: string; } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 14018b7d66..21ce890b46 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -15,10 +15,18 @@ import * as onExit from './on-exit'; import * as wire from './serialization'; import * as tar from './tar-cache'; -export class JsiiFault extends Error { +export enum JsiiErrorType { + JSII_FAULT = 'jsii-fault', +} + +export interface JsiiError extends Error { + readonly type: JsiiErrorType; +} + +export class JsiiFault extends Error implements JsiiError { + public readonly type = JsiiErrorType.JSII_FAULT; public constructor(message: string) { super(message); - this.name = 'JsiiFault'; } } diff --git a/packages/@jsii/kernel/src/objects.ts b/packages/@jsii/kernel/src/objects.ts index 921c4dcf77..425420a3af 100644 --- a/packages/@jsii/kernel/src/objects.ts +++ b/packages/@jsii/kernel/src/objects.ts @@ -1,8 +1,8 @@ import * as spec from '@jsii/spec'; import * as api from './api'; -import { EMPTY_OBJECT_FQN } from './serialization'; import { JsiiFault } from './kernel'; +import { EMPTY_OBJECT_FQN } from './serialization'; /** * Symbol under which we store the { type -> objid } map on object instances @@ -180,7 +180,9 @@ export class ObjectTable { */ public findObject(objref: api.ObjRef): RegisteredObject { if (typeof objref !== 'object' || !(api.TOKEN_REF in objref)) { - throw new JsiiFault(`Malformed object reference: ${JSON.stringify(objref)}`); + throw new JsiiFault( + `Malformed object reference: ${JSON.stringify(objref)}`, + ); } const objid = objref[api.TOKEN_REF]; diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index e660271e01..2612863bfe 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,4 @@ -import { api, Kernel, JsiiFault } from '@jsii/kernel'; +import { api, Kernel, JsiiFault, JsiiError } from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -152,7 +152,7 @@ export class KernelHost { } this.writeOkay(ret); - } catch (e) { + } catch (e: any) { this.writeError(e); } @@ -179,11 +179,12 @@ export class KernelHost { /** * Writes an "error" result to stdout. */ - private writeError(error: any) { - const res = { error: error.message, stack: undefined }; - if (!this.opts.noStack) { - res.stack = error.stack; - } + private writeError(error: JsiiError) { + const res = { + error: error.message, + type: error.type, + stack: this.opts.noStack ? undefined : error.stack, + }; this.inout.write(res); } From d8510a759a66ee8169d7e3f26805c7df2e2083a3 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 8 Sep 2022 14:09:47 -0700 Subject: [PATCH 03/39] playback tests pass --- packages/@jsii/kernel/src/recording.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 6827369276..d5eef79198 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -50,7 +50,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { ok(value); }) .catch((err: any) => { - logOutput({ error: err.message }); + logOutput({ error: err.message, type: err.type }); fail(err); }); }); @@ -59,7 +59,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { logOutput({ ok: ret }); return ret; } catch (e: any) { - logOutput({ error: e.message }); + logOutput({ error: e.message, type: e.type }); throw new JsiiFault(e.message); } }, From 770c120817b8b0276bcc839354042e136523656e Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 8 Sep 2022 15:48:52 -0700 Subject: [PATCH 04/39] remove comment --- packages/@jsii/kernel/src/api.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/@jsii/kernel/src/api.ts b/packages/@jsii/kernel/src/api.ts index 7b0f0d845b..b339a4716b 100644 --- a/packages/@jsii/kernel/src/api.ts +++ b/packages/@jsii/kernel/src/api.ts @@ -1,5 +1,3 @@ -//import { JsiiErrorType } from './kernel'; - export const TOKEN_REF = '$jsii.byref'; export const TOKEN_INTERFACES = '$jsii.interfaces'; export const TOKEN_DATE = '$jsii.date'; @@ -278,6 +276,5 @@ export interface OkayResponse { export interface ErrorResponse { readonly error: string; - //readonly type: JsiiErrorType; readonly stack?: string; } From 8b81f623e5ab31519e31f34f1018dad3a9f8de91 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Fri, 9 Sep 2022 16:03:35 -0700 Subject: [PATCH 05/39] js exception added --- .../software/amazon/jsii/JsException.java | 33 ++++++++++++ .../main/java/software/amazon/jsii/JsiiE.java | 47 +++++++++++++++++ .../software/amazon/jsii/JsiiException.java | 52 +++++++++---------- .../software/amazon/jsii/JsiiRuntime.java | 8 +++ packages/@jsii/kernel/src/kernel.ts | 1 + 5 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java create mode 100644 packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java new file mode 100644 index 0000000000..f86f7d091b --- /dev/null +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java @@ -0,0 +1,33 @@ +package software.amazon.jsii; + +/* + * Represents an irrecoverable error, such as a + * broken pipe. + */ +public final class JsException extends JsiiE { + + /** + * Creates an exception. + * @param message The error message + */ + JsException(final String message) { + super(message); + } + + /** + * Creates an exception. + * @param e The error that caused this exception + */ + JsException(final Throwable e) { + super(e); + } + + /** + * Creates an exception. + * @param message The error message + * @param e The error that caused this exception + */ + JsException(final String message, final Throwable e) { + super(message, e); + } +} diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java new file mode 100644 index 0000000000..42620bc4b8 --- /dev/null +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java @@ -0,0 +1,47 @@ +package software.amazon.jsii; + +/** + * An error raised by the jsii runtime. + */ +public abstract class JsiiE extends RuntimeException { + public static enum Type { + JSII_FAULT("jsii-fault"), + JS_EXCEPTION("js-error"); + + private final String errorType; + + Type(String str) { + this.errorType = str; + } + + public String toString() { + return this.errorType; + } + } + public static final long serialVersionUID = 1L; + + /** + * Creates an exception. + * @param message The error message + */ + JsiiE(final String message) { + super(message); + } + + /** + * Creates an exception. + * @param e The error that caused this exception + */ + JsiiE(final Throwable e) { + super(e); + } + + /** + * Creates an exception. + * @param message The error message + * @param e The error that caused this exception + */ + JsiiE(final String message, final Throwable e) { + super(message, e); + } +} diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index 34361bdaa8..c7c9eb4d16 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -1,33 +1,33 @@ package software.amazon.jsii; -/** - * An error raised by the jsii runtime. +/* + * Represents an irrecoverable error, such as a + * broken pipe. */ -public final class JsiiException extends RuntimeException { - public static final long serialVersionUID = 1L; +public final class JsiiException extends JsiiE { - /** - * Creates an exception. - * @param message The error message - */ - JsiiException(final String message) { - super(message); - } + /** + * Creates an exception. + * @param message The error message + */ + JsiiException(final String message) { + super(message); + } - /** - * Creates an exception. - * @param e The error that caused this exception - */ - JsiiException(final Throwable e) { - super(e); - } + /** + * Creates an exception. + * @param e The error that caused this exception + */ + JsiiException(final Throwable e) { + super(e); + } - /** - * Creates an exception. - * @param message The error message - * @param e The error that caused this exception - */ - JsiiException(final String message, final Throwable e) { - super(message, e); - } + /** + * Creates an exception. + * @param message The error message + * @param e The error that caused this exception + */ + JsiiException(final String message, final Throwable e) { + super(message, e); + } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index bfda1b8de9..279e2d2884 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -116,11 +116,19 @@ JsonNode requestResponse(final JsonNode request) { * @return Never */ private JsonNode processErrorResponse(final JsonNode resp) { + String errorType = resp.get("type").asText(); String errorMessage = resp.get("error").asText(); if (resp.has("stack")) { errorMessage += "\n" + resp.get("stack").asText(); } + if (errorType.equals(JsiiException.Type.JS_EXCEPTION.toString())) { + throw new JsException(errorMessage); + } + + // TODO: check for the type checking exception here + + // default to JsiiException throw new JsiiException(errorMessage); } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 21ce890b46..b92f247c18 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -17,6 +17,7 @@ import * as tar from './tar-cache'; export enum JsiiErrorType { JSII_FAULT = 'jsii-fault', + JS_ERROR = 'js-error', } export interface JsiiError extends Error { From 3c0dc7697f95c927449668d65e6f50e1468f7d12 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 12 Sep 2022 13:52:25 -0700 Subject: [PATCH 06/39] rename --- .../amazon/jsii/{JsiiE.java => JsiiBaseException.java} | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/{JsiiE.java => JsiiBaseException.java} (74%) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java similarity index 74% rename from packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java rename to packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java index 42620bc4b8..58cc29675c 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiE.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java @@ -3,10 +3,11 @@ /** * An error raised by the jsii runtime. */ -public abstract class JsiiE extends RuntimeException { +public abstract class JsiiBaseException extends RuntimeException { public static enum Type { JSII_FAULT("jsii-fault"), JS_EXCEPTION("js-error"); + // TYPE_CHECKING_EXCEPTION("type-checking-exception"); private final String errorType; @@ -24,7 +25,7 @@ public String toString() { * Creates an exception. * @param message The error message */ - JsiiE(final String message) { + JsiiBaseException(final String message) { super(message); } @@ -32,7 +33,7 @@ public String toString() { * Creates an exception. * @param e The error that caused this exception */ - JsiiE(final Throwable e) { + JsiiBaseException(final Throwable e) { super(e); } @@ -41,7 +42,7 @@ public String toString() { * @param message The error message * @param e The error that caused this exception */ - JsiiE(final String message, final Throwable e) { + JsiiBaseException(final String message, final Throwable e) { super(message, e); } } From 3ed3b27b55550523cd0a59165cfe17a78d568fb3 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 12 Sep 2022 14:42:28 -0700 Subject: [PATCH 07/39] rename JsiiE --- .../test/java/software/amazon/jsii/testing/ComplianceTest.java | 2 +- .../project/src/main/java/software/amazon/jsii/JsException.java | 2 +- .../src/main/java/software/amazon/jsii/JsiiException.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 673397e3ef..18de9352d2 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -335,7 +335,7 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { calc3.add(10); } - catch (Exception e) { thrown = true; } + catch (JsiiException e) { thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java index f86f7d091b..926910b7c2 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java @@ -4,7 +4,7 @@ * Represents an irrecoverable error, such as a * broken pipe. */ -public final class JsException extends JsiiE { +public final class JsException extends JsiiBaseException { /** * Creates an exception. diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index c7c9eb4d16..6c8b303a1d 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -4,7 +4,7 @@ * Represents an irrecoverable error, such as a * broken pipe. */ -public final class JsiiException extends JsiiE { +public final class JsiiException extends JsiiBaseException { /** * Creates an exception. From d6975d800b696ec50de61260c6ee96f6cd7deb6b Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 12 Sep 2022 16:35:29 -0700 Subject: [PATCH 08/39] need to ensure that JsErrors are thrown correctly, the writeError() method can't distinguish them yet --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 3 ++- .../main/java/software/amazon/jsii/JsiiBaseException.java | 1 + .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 2 +- packages/@jsii/kernel/src/api.ts | 3 +++ packages/@jsii/kernel/src/kernel.ts | 6 ++++++ packages/@jsii/runtime/lib/host.ts | 1 + packages/jsii-calc/lib/compliance.ts | 2 +- 7 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 18de9352d2..2f68e0a15d 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import software.amazon.jsii.ComplianceSuiteHarness; +import software.amazon.jsii.JsException; import software.amazon.jsii.JsiiEngine; import software.amazon.jsii.JsiiException; import software.amazon.jsii.ReloadingClassLoader; @@ -335,7 +336,7 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { calc3.add(10); } - catch (JsiiException e) { thrown = true; } + catch (JsException e) { e.printStackTrace(); thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java index 58cc29675c..bdb27721d4 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java @@ -20,6 +20,7 @@ public String toString() { } } public static final long serialVersionUID = 1L; + //public final Type type; /** * Creates an exception. diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 279e2d2884..bcd76af30f 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -110,7 +110,7 @@ JsonNode requestResponse(final JsonNode request) { /** * Handles an "error" response by extracting the message and stack trace - * and throwing a JsiiException. + * and throwing a JsiiException or a JsException. * * @param resp The response * @return Never diff --git a/packages/@jsii/kernel/src/api.ts b/packages/@jsii/kernel/src/api.ts index b339a4716b..7b0f0d845b 100644 --- a/packages/@jsii/kernel/src/api.ts +++ b/packages/@jsii/kernel/src/api.ts @@ -1,3 +1,5 @@ +//import { JsiiErrorType } from './kernel'; + export const TOKEN_REF = '$jsii.byref'; export const TOKEN_INTERFACES = '$jsii.interfaces'; export const TOKEN_DATE = '$jsii.date'; @@ -276,5 +278,6 @@ export interface OkayResponse { export interface ErrorResponse { readonly error: string; + //readonly type: JsiiErrorType; readonly stack?: string; } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index b92f247c18..1b47cbeec1 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -31,6 +31,12 @@ export class JsiiFault extends Error implements JsiiError { } } +export class JsError extends Error implements JsiiError { + public readonly type = JsiiErrorType.JS_ERROR; + public constructor(message: string) { + super(message); + } +} export class Kernel { /** * Set to true for verbose debugging. diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 2612863bfe..062653cc2b 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -180,6 +180,7 @@ export class KernelHost { * Writes an "error" result to stdout. */ private writeError(error: JsiiError) { + console.log(error) const res = { error: error.message, type: error.type, diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index 53ed371f3f..16b91abb62 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -654,7 +654,7 @@ export class Thrower { } private doThrowError() { - throw new Error(); + throw new Error(); // TODO: need to ensure that this is a JSError: playback sees it, and it should have `type: js-error`, not `jsii-fault`. } } From 6ed003bc4e47cffd7ffba9e8c57143d9a8395332 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 12:20:45 -0700 Subject: [PATCH 09/39] playback fix --- packages/@jsii/kernel/src/recording.ts | 5 ++++- packages/@jsii/runtime/lib/host.ts | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index d5eef79198..471981326f 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -1,6 +1,6 @@ import * as fs from 'fs-extra'; -import { JsiiFault, Kernel } from './kernel'; +import { JsError, JsiiErrorType, JsiiFault, Kernel } from './kernel'; export async function closeRecording(kernel: Kernel): Promise { const logfile: fs.WriteStream = (kernel as any).logfile; @@ -60,6 +60,9 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { return ret; } catch (e: any) { logOutput({ error: e.message, type: e.type }); + if (e.type === JsiiErrorType.JS_ERROR) { + throw new JsError(e); + } throw new JsiiFault(e.message); } }, diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 062653cc2b..2612863bfe 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -180,7 +180,6 @@ export class KernelHost { * Writes an "error" result to stdout. */ private writeError(error: JsiiError) { - console.log(error) const res = { error: error.message, type: error.type, From 428c95e69e97eb800f13bbe65d18b985327ddcea Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 14:33:17 -0700 Subject: [PATCH 10/39] force js error from kernel host...pretty sure this is the wrong approach, needs to be fixed before this can be merged --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 2 +- packages/@jsii/runtime/lib/host.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 2f68e0a15d..0c38ea97da 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -336,7 +336,7 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { calc3.add(10); } - catch (JsException e) { e.printStackTrace(); thrown = true; } + catch (JsException e) { thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 2612863bfe..0e7c37c421 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,4 @@ -import { api, Kernel, JsiiFault, JsiiError } from '@jsii/kernel'; +import { api, Kernel, JsiiFault, JsiiError, JsiiErrorType } from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -182,7 +182,7 @@ export class KernelHost { private writeError(error: JsiiError) { const res = { error: error.message, - type: error.type, + type: error.type ?? JsiiErrorType.JS_ERROR, // TODO: this doesn't feel like the right place to determine JS_ERRORs... stack: this.opts.noStack ? undefined : error.stack, }; this.inout.write(res); From 8dbb87f3e81582eed2e92d63bf1664323a9d165a Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 16:11:53 -0700 Subject: [PATCH 11/39] working userland errors --- packages/@jsii/kernel/src/kernel.ts | 7 +++++++ packages/@jsii/runtime/lib/host.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 1b47cbeec1..fab27f5f3b 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -1306,6 +1306,13 @@ export class Kernel { this.syncInProgress = desc; try { return fn(); + } catch (e: any) { + if (e.type === JsiiErrorType.JSII_FAULT) { + throw new JsiiFault(e); + } + // default to JsError because any JsiiFaults have been emitted from the kernel + // with their type field populated before the request was made to the js code. + throw new JsError(e); } finally { delete this.syncInProgress; } diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 0e7c37c421..2612863bfe 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,4 @@ -import { api, Kernel, JsiiFault, JsiiError, JsiiErrorType } from '@jsii/kernel'; +import { api, Kernel, JsiiFault, JsiiError } from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -182,7 +182,7 @@ export class KernelHost { private writeError(error: JsiiError) { const res = { error: error.message, - type: error.type ?? JsiiErrorType.JS_ERROR, // TODO: this doesn't feel like the right place to determine JS_ERRORs... + type: error.type, stack: this.opts.noStack ? undefined : error.stack, }; this.inout.write(res); From 34865734ec19250c307fb901c7a2783e9f55c5d7 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 16:13:58 -0700 Subject: [PATCH 12/39] remove old commment --- packages/jsii-calc/lib/compliance.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsii-calc/lib/compliance.ts b/packages/jsii-calc/lib/compliance.ts index 16b91abb62..53ed371f3f 100644 --- a/packages/jsii-calc/lib/compliance.ts +++ b/packages/jsii-calc/lib/compliance.ts @@ -654,7 +654,7 @@ export class Thrower { } private doThrowError() { - throw new Error(); // TODO: need to ensure that this is a JSError: playback sees it, and it should have `type: js-error`, not `jsii-fault`. + throw new Error(); } } From 42a4f31cb2f89adf384730e8857d496d6fa7f3e1 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 16:54:14 -0700 Subject: [PATCH 13/39] remove commented code --- .../src/main/java/software/amazon/jsii/JsiiBaseException.java | 2 -- packages/@jsii/kernel/src/api.ts | 3 --- 2 files changed, 5 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java index bdb27721d4..d4543db38c 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java @@ -7,7 +7,6 @@ public abstract class JsiiBaseException extends RuntimeException { public static enum Type { JSII_FAULT("jsii-fault"), JS_EXCEPTION("js-error"); - // TYPE_CHECKING_EXCEPTION("type-checking-exception"); private final String errorType; @@ -20,7 +19,6 @@ public String toString() { } } public static final long serialVersionUID = 1L; - //public final Type type; /** * Creates an exception. diff --git a/packages/@jsii/kernel/src/api.ts b/packages/@jsii/kernel/src/api.ts index 7b0f0d845b..b339a4716b 100644 --- a/packages/@jsii/kernel/src/api.ts +++ b/packages/@jsii/kernel/src/api.ts @@ -1,5 +1,3 @@ -//import { JsiiErrorType } from './kernel'; - export const TOKEN_REF = '$jsii.byref'; export const TOKEN_INTERFACES = '$jsii.interfaces'; export const TOKEN_DATE = '$jsii.date'; @@ -278,6 +276,5 @@ export interface OkayResponse { export interface ErrorResponse { readonly error: string; - //readonly type: JsiiErrorType; readonly stack?: string; } From a32e01d76f51917ff982a5dab102f44d7c6b473c Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 13 Sep 2022 17:42:09 -0700 Subject: [PATCH 14/39] test fix --- .../src/main/java/software/amazon/jsii/JsiiException.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index 6c8b303a1d..d9ad239218 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -1,8 +1,7 @@ package software.amazon.jsii; /* - * Represents an irrecoverable error, such as a - * broken pipe. + * Represents an irrecoverable error, likely thrown from the kernel. */ public final class JsiiException extends JsiiBaseException { From f116ddbff3bbdcccac815113609d5bd5357d88dd Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 14 Sep 2022 14:32:33 -0700 Subject: [PATCH 15/39] removed old comment --- .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index bcd76af30f..0cbd6d1da4 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -126,9 +126,6 @@ private JsonNode processErrorResponse(final JsonNode resp) { throw new JsException(errorMessage); } - // TODO: check for the type checking exception here - - // default to JsiiException throw new JsiiException(errorMessage); } From feecb62a1c54f79668327d0bd9b9877cf1165645 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 14 Sep 2022 14:41:13 -0700 Subject: [PATCH 16/39] updated expected exception type to JsiiException --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 0c38ea97da..bcce16a6dd 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -519,7 +519,7 @@ public String getTheProperty() { boolean thrown = false; try { so.retrieveValueOfTheProperty(); - } catch (Exception e) { + } catch (JsiiException e) { assertTrue(e.getMessage().contains("Oh no, this is bad")); thrown = true; } @@ -537,7 +537,7 @@ public void setTheProperty(String value) { boolean thrown = false; try { so.modifyValueOfTheProperty("Hii"); - } catch (Exception e) { + } catch (JsiiException e) { assertTrue(e.getMessage().contains("Exception from overloaded setter")); thrown = true; } From da967df4c94e816ad0b5a45e24fe50d322f36063 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 14 Sep 2022 16:01:50 -0700 Subject: [PATCH 17/39] updated type to name --- .../java/software/amazon/jsii/JsiiBaseException.java | 4 ++-- .../main/java/software/amazon/jsii/JsiiRuntime.java | 4 ++-- packages/@jsii/kernel/src/kernel.ts | 12 ++++++------ packages/@jsii/kernel/src/recording.ts | 6 +++--- packages/@jsii/runtime/lib/host.ts | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java index d4543db38c..ff7273a877 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java @@ -5,8 +5,8 @@ */ public abstract class JsiiBaseException extends RuntimeException { public static enum Type { - JSII_FAULT("jsii-fault"), - JS_EXCEPTION("js-error"); + JSII_FAULT("@jsii/kernel.Fault"), + JS_EXCEPTION("@jsii/kernel.JSError"); private final String errorType; diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 0cbd6d1da4..5482eed384 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -116,13 +116,13 @@ JsonNode requestResponse(final JsonNode request) { * @return Never */ private JsonNode processErrorResponse(final JsonNode resp) { - String errorType = resp.get("type").asText(); + String errorName = resp.get("name").asText(); String errorMessage = resp.get("error").asText(); if (resp.has("stack")) { errorMessage += "\n" + resp.get("stack").asText(); } - if (errorType.equals(JsiiException.Type.JS_EXCEPTION.toString())) { + if (errorName.equals(JsiiException.Type.JS_EXCEPTION.toString())) { throw new JsException(errorMessage); } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index fab27f5f3b..2b5434ac08 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -16,23 +16,23 @@ import * as wire from './serialization'; import * as tar from './tar-cache'; export enum JsiiErrorType { - JSII_FAULT = 'jsii-fault', - JS_ERROR = 'js-error', + JSII_FAULT = '@jsii/kernel.Fault', + JS_ERROR = '@jsii/kernel.JSError', } export interface JsiiError extends Error { - readonly type: JsiiErrorType; + readonly name: JsiiErrorType; } export class JsiiFault extends Error implements JsiiError { - public readonly type = JsiiErrorType.JSII_FAULT; + public readonly name = JsiiErrorType.JSII_FAULT; public constructor(message: string) { super(message); } } export class JsError extends Error implements JsiiError { - public readonly type = JsiiErrorType.JS_ERROR; + public readonly name = JsiiErrorType.JS_ERROR; public constructor(message: string) { super(message); } @@ -1307,7 +1307,7 @@ export class Kernel { try { return fn(); } catch (e: any) { - if (e.type === JsiiErrorType.JSII_FAULT) { + if (e.name === JsiiErrorType.JSII_FAULT) { throw new JsiiFault(e); } // default to JsError because any JsiiFaults have been emitted from the kernel diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 471981326f..5a5d676ebe 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -50,7 +50,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { ok(value); }) .catch((err: any) => { - logOutput({ error: err.message, type: err.type }); + logOutput({ error: err.message, name: err.name }); fail(err); }); }); @@ -59,9 +59,9 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { logOutput({ ok: ret }); return ret; } catch (e: any) { - logOutput({ error: e.message, type: e.type }); + logOutput({ error: e.message, name: e.name }); if (e.type === JsiiErrorType.JS_ERROR) { - throw new JsError(e); + throw new JsError(e.message); } throw new JsiiFault(e.message); } diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 2612863bfe..e12d33f418 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -182,7 +182,7 @@ export class KernelHost { private writeError(error: JsiiError) { const res = { error: error.message, - type: error.type, + name: error.name, stack: this.opts.noStack ? undefined : error.stack, }; this.inout.write(res); From 855085d7a75e3845631b1e5947cf1a5292f98f49 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 14 Sep 2022 17:24:36 -0700 Subject: [PATCH 18/39] updated comment --- packages/@jsii/kernel/src/kernel.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 2b5434ac08..7a255fc76a 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -1310,8 +1310,10 @@ export class Kernel { if (e.name === JsiiErrorType.JSII_FAULT) { throw new JsiiFault(e); } - // default to JsError because any JsiiFaults have been emitted from the kernel - // with their type field populated before the request was made to the js code. + // This error comes from the kernel, which means it can either be an error + // thrown by the kernel directly, or an error that the kernel is rethrowing from + // user code. If the error comes from the kernel, then its name field will be populated; + // this implies that the name field is only empty for user code. throw new JsError(e); } finally { delete this.syncInProgress; From 53aa68d7035a3783a5c57bb52fec423cda6d2c67 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 15 Sep 2022 11:21:02 -0700 Subject: [PATCH 19/39] updated comment --- .../src/main/java/software/amazon/jsii/JsException.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java index 926910b7c2..ab88c88808 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java @@ -1,8 +1,7 @@ package software.amazon.jsii; /* - * Represents an irrecoverable error, such as a - * broken pipe. + * Represents an error thrown from the user JS library. */ public final class JsException extends JsiiBaseException { From 3fc61f804a62b0141f64245dd01ae952deef7560 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 15 Sep 2022 11:25:26 -0700 Subject: [PATCH 20/39] final comment update --- packages/@jsii/kernel/src/kernel.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 7a255fc76a..86ee43aff7 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -1310,10 +1310,9 @@ export class Kernel { if (e.name === JsiiErrorType.JSII_FAULT) { throw new JsiiFault(e); } - // This error comes from the kernel, which means it can either be an error - // thrown by the kernel directly, or an error that the kernel is rethrowing from - // user code. If the error comes from the kernel, then its name field will be populated; - // this implies that the name field is only empty for user code. + // This error can be thrown by the kernel directly, or it can be + // thrown from user code. If the error comes from the kernel, then its name field will be populated; + // if the error comes from user code, the name field will not be populated. throw new JsError(e); } finally { delete this.syncInProgress; From dc7562c2736ddaa58b22958d8b923973b5dea47e Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 15 Sep 2022 11:32:54 -0700 Subject: [PATCH 21/39] rename JsError to JSError --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 4 ++-- .../src/main/java/software/amazon/jsii/JsException.java | 8 ++++---- .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 4 ++-- packages/@jsii/kernel/src/kernel.ts | 4 ++-- packages/@jsii/kernel/src/recording.ts | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index bcce16a6dd..c5116709da 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import software.amazon.jsii.ComplianceSuiteHarness; -import software.amazon.jsii.JsException; +import software.amazon.jsii.JSException; import software.amazon.jsii.JsiiEngine; import software.amazon.jsii.JsiiException; import software.amazon.jsii.ReloadingClassLoader; @@ -336,7 +336,7 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { calc3.add(10); } - catch (JsException e) { thrown = true; } + catch (JSException e) { thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java index ab88c88808..80e21beff9 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java @@ -3,13 +3,13 @@ /* * Represents an error thrown from the user JS library. */ -public final class JsException extends JsiiBaseException { +public final class JSException extends JsiiBaseException { /** * Creates an exception. * @param message The error message */ - JsException(final String message) { + JSException(final String message) { super(message); } @@ -17,7 +17,7 @@ public final class JsException extends JsiiBaseException { * Creates an exception. * @param e The error that caused this exception */ - JsException(final Throwable e) { + JSException(final Throwable e) { super(e); } @@ -26,7 +26,7 @@ public final class JsException extends JsiiBaseException { * @param message The error message * @param e The error that caused this exception */ - JsException(final String message, final Throwable e) { + JSException(final String message, final Throwable e) { super(message, e); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 5482eed384..19f277fa99 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -110,7 +110,7 @@ JsonNode requestResponse(final JsonNode request) { /** * Handles an "error" response by extracting the message and stack trace - * and throwing a JsiiException or a JsException. + * and throwing a JsiiException or a JSException. * * @param resp The response * @return Never @@ -123,7 +123,7 @@ private JsonNode processErrorResponse(final JsonNode resp) { } if (errorName.equals(JsiiException.Type.JS_EXCEPTION.toString())) { - throw new JsException(errorMessage); + throw new JSException(errorMessage); } throw new JsiiException(errorMessage); diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 86ee43aff7..a3891b7ff0 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -31,7 +31,7 @@ export class JsiiFault extends Error implements JsiiError { } } -export class JsError extends Error implements JsiiError { +export class JSError extends Error implements JsiiError { public readonly name = JsiiErrorType.JS_ERROR; public constructor(message: string) { super(message); @@ -1313,7 +1313,7 @@ export class Kernel { // This error can be thrown by the kernel directly, or it can be // thrown from user code. If the error comes from the kernel, then its name field will be populated; // if the error comes from user code, the name field will not be populated. - throw new JsError(e); + throw new JSError(e); } finally { delete this.syncInProgress; } diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 5a5d676ebe..55d3c4be8b 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -1,6 +1,6 @@ import * as fs from 'fs-extra'; -import { JsError, JsiiErrorType, JsiiFault, Kernel } from './kernel'; +import { JSError, JsiiErrorType, JsiiFault, Kernel } from './kernel'; export async function closeRecording(kernel: Kernel): Promise { const logfile: fs.WriteStream = (kernel as any).logfile; @@ -61,7 +61,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { } catch (e: any) { logOutput({ error: e.message, name: e.name }); if (e.type === JsiiErrorType.JS_ERROR) { - throw new JsError(e.message); + throw new JSError(e.message); } throw new JsiiFault(e.message); } From fb8a93f355105e286db266ffeb8cfea937e14deb Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 15 Sep 2022 11:37:44 -0700 Subject: [PATCH 22/39] have to delete the file to change the capitalization...why???? --- .../software/amazon/jsii/JsException.java | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java deleted file mode 100644 index 80e21beff9..0000000000 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsException.java +++ /dev/null @@ -1,32 +0,0 @@ -package software.amazon.jsii; - -/* - * Represents an error thrown from the user JS library. - */ -public final class JSException extends JsiiBaseException { - - /** - * Creates an exception. - * @param message The error message - */ - JSException(final String message) { - super(message); - } - - /** - * Creates an exception. - * @param e The error that caused this exception - */ - JSException(final Throwable e) { - super(e); - } - - /** - * Creates an exception. - * @param message The error message - * @param e The error that caused this exception - */ - JSException(final String message, final Throwable e) { - super(message, e); - } -} From a468d9071ca61012ce7b6faa004af2525e135d0a Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 15 Sep 2022 11:38:13 -0700 Subject: [PATCH 23/39] final rename --- .../software/amazon/jsii/JSException.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java new file mode 100644 index 0000000000..80e21beff9 --- /dev/null +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java @@ -0,0 +1,32 @@ +package software.amazon.jsii; + +/* + * Represents an error thrown from the user JS library. + */ +public final class JSException extends JsiiBaseException { + + /** + * Creates an exception. + * @param message The error message + */ + JSException(final String message) { + super(message); + } + + /** + * Creates an exception. + * @param e The error that caused this exception + */ + JSException(final Throwable e) { + super(e); + } + + /** + * Creates an exception. + * @param message The error message + * @param e The error that caused this exception + */ + JSException(final String message, final Throwable e) { + super(message, e); + } +} From 373cae25ee3041002e3e69aaaff848f4917cbc15 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 19 Sep 2022 10:38:29 -0700 Subject: [PATCH 24/39] Made JsiiException the base error class in Java. JsiiError now represents the fault. JSException is now java.lang.RuntimeException. --- .../amazon/jsii/testing/ComplianceTest.java | 9 ++-- .../@jsii/java-runtime/BundledRuntime.t.js | 2 +- .../amazon/jsii/JsiiBaseException.java | 47 ------------------- .../java/software/amazon/jsii/JsiiClient.java | 4 +- .../java/software/amazon/jsii/JsiiEngine.java | 44 ++++++++--------- .../jsii/{JSException.java => JsiiError.java} | 14 +++--- .../software/amazon/jsii/JsiiException.java | 20 +++++++- .../java/software/amazon/jsii/JsiiObject.java | 8 ++-- .../amazon/jsii/JsiiObjectMapper.java | 4 +- .../software/amazon/jsii/JsiiObjectRef.java | 2 +- .../software/amazon/jsii/JsiiRuntime.java | 28 +++++------ .../java/software/amazon/jsii/UnsafeCast.java | 6 +-- .../main/java/software/amazon/jsii/Util.java | 4 +- packages/@jsii/kernel/src/kernel.ts | 8 ++-- packages/@jsii/kernel/src/recording.ts | 6 +-- 15 files changed, 87 insertions(+), 119 deletions(-) delete mode 100644 packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java rename packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/{JSException.java => JsiiError.java} (62%) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index c5116709da..3d02c262de 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import software.amazon.jsii.ComplianceSuiteHarness; -import software.amazon.jsii.JSException; import software.amazon.jsii.JsiiEngine; import software.amazon.jsii.JsiiException; import software.amazon.jsii.ReloadingClassLoader; @@ -336,7 +335,7 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { calc3.add(10); } - catch (JSException e) { thrown = true; } + catch (RuntimeException e) { thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); @@ -450,7 +449,7 @@ public java.lang.Number overrideMe(java.lang.Number mult) { boolean thrown = false; try { obj.callMe(); - } catch (JsiiException e) { + } catch (RuntimeException e) { assertTrue(e.getMessage().contains( "Thrown by native code")); thrown = true; } @@ -519,7 +518,7 @@ public String getTheProperty() { boolean thrown = false; try { so.retrieveValueOfTheProperty(); - } catch (JsiiException e) { + } catch (RuntimeException e) { assertTrue(e.getMessage().contains("Oh no, this is bad")); thrown = true; } @@ -537,7 +536,7 @@ public void setTheProperty(String value) { boolean thrown = false; try { so.modifyValueOfTheProperty("Hii"); - } catch (JsiiException e) { + } catch (RuntimeException e) { assertTrue(e.getMessage().contains("Exception from overloaded setter")); thrown = true; } diff --git a/packages/@jsii/java-runtime/BundledRuntime.t.js b/packages/@jsii/java-runtime/BundledRuntime.t.js index 5bc380d0fd..daf6890ddd 100644 --- a/packages/@jsii/java-runtime/BundledRuntime.t.js +++ b/packages/@jsii/java-runtime/BundledRuntime.t.js @@ -34,7 +34,7 @@ ${resources.map(extractResource).map(indent(12)).join('\n')} return entrypoint.toString(); } catch (final IOException ioe) { - throw new JsiiException("Unable to extract bundled @jsii/runtime library", ioe); + throw new JsiiError("Unable to extract bundled @jsii/runtime library", ioe); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java deleted file mode 100644 index ff7273a877..0000000000 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiBaseException.java +++ /dev/null @@ -1,47 +0,0 @@ -package software.amazon.jsii; - -/** - * An error raised by the jsii runtime. - */ -public abstract class JsiiBaseException extends RuntimeException { - public static enum Type { - JSII_FAULT("@jsii/kernel.Fault"), - JS_EXCEPTION("@jsii/kernel.JSError"); - - private final String errorType; - - Type(String str) { - this.errorType = str; - } - - public String toString() { - return this.errorType; - } - } - public static final long serialVersionUID = 1L; - - /** - * Creates an exception. - * @param message The error message - */ - JsiiBaseException(final String message) { - super(message); - } - - /** - * Creates an exception. - * @param e The error that caused this exception - */ - JsiiBaseException(final Throwable e) { - super(e); - } - - /** - * Creates an exception. - * @param message The error message - * @param e The error that caused this exception - */ - JsiiBaseException(final String message, final Throwable e) { - super(message, e); - } -} diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java index b730bcfa63..9dbbad425d 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java @@ -59,7 +59,7 @@ public void loadModule(final JsiiModule module) { Files.delete(tarball.getParent()); } } catch (IOException e) { - throw new JsiiException("Unable to extract resource " + module.getBundleResourceName(), e); + throw new JsiiError("Unable to extract resource " + module.getBundleResourceName(), e); } } @@ -226,7 +226,7 @@ public List pendingCallbacks() { JsonNode callbacksResp = resp.get("callbacks"); if (callbacksResp == null || !callbacksResp.isArray()) { - throw new JsiiException("Expecting a 'callbacks' key with an array in response"); + throw new JsiiError("Expecting a 'callbacks' key with an array in response"); } ArrayNode callbacksArray = (ArrayNode) callbacksResp; diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index 225a98a136..c4222013b4 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -164,7 +164,7 @@ private JsiiEngine() { */ public void loadModule(final Class moduleClass) { if (!JsiiModule.class.isAssignableFrom(moduleClass)) { - throw new JsiiException("Invalid module class " + throw new JsiiError("Invalid module class " + moduleClass.getName() + ". It must be derived from JsiiModule"); } @@ -173,7 +173,7 @@ public void loadModule(final Class moduleClass) { try { module = moduleClass.getConstructor().newInstance(); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { - throw new JsiiException(e); + throw new JsiiError(e); } if (this.loadedModules.containsKey(module.getModuleName())) { @@ -270,12 +270,12 @@ public JsiiObjectRef nativeToObjRef(final Object nativeObject) { * * @param objRef The object reference * @return a JsiiObject - * @throws JsiiException If the object is not found. + * @throws JsiiError If the object is not found. */ public Object getObject(final JsiiObjectRef objRef) { Object obj = this.objects.get(objRef.getObjId()); if (obj == null) { - throw new JsiiException("Cannot find jsii object: " + objRef.getObjId()); + throw new JsiiError("Cannot find jsii object: " + objRef.getObjId()); } return obj; } @@ -303,24 +303,24 @@ Class resolveJavaClass(final String fqn) { } String[] parts = fqn.split("\\."); if (parts.length < 2) { - throw new JsiiException("Malformed FQN: " + fqn); + throw new JsiiError("Malformed FQN: " + fqn); } String moduleName = parts[0]; JsonNode names = this.getClient().getModuleNames(moduleName); if (!names.has("java")) { - throw new JsiiException("No java name for module " + moduleName); + throw new JsiiError("No java name for module " + moduleName); } final JsiiModule module = this.loadedModules.get(moduleName); if (module == null) { - throw new JsiiException("No loaded module is named " + moduleName); + throw new JsiiError("No loaded module is named " + moduleName); } try { return module.resolveClass(fqn); } catch (final ClassNotFoundException cfne) { - throw new JsiiException(cfne); + throw new JsiiError(cfne); } } @@ -346,12 +346,12 @@ private JsiiObject createNativeProxy(final String fqn, final JsiiObjectRef objRe ctor.setAccessible(false); return newObj; } catch (NoSuchMethodException e) { - throw new JsiiException("Cannot create native object of type " + throw new JsiiError("Cannot create native object of type " + klass.getName() + " without a constructor that accepts an InitializationMode argument", e); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { - throw new JsiiException("Unable to instantiate a new object for FQN " + fqn + ": " + throw new JsiiError("Unable to instantiate a new object for FQN " + fqn + ": " + e.getMessage(), e); } } catch (ClassNotFoundException e) { @@ -369,7 +369,7 @@ private JsiiObject createNativeProxy(final String fqn, final JsiiObjectRef objRe public Enum findEnumValue(final String enumRef) { int sep = enumRef.lastIndexOf('/'); if (sep == -1) { - throw new JsiiException("Malformed enum reference: " + enumRef); + throw new JsiiError("Malformed enum reference: " + enumRef); } String typeName = enumRef.substring(0, sep); @@ -396,7 +396,7 @@ public void processAllPendingCallbacks() { * Invokes a local callback and returns the result/error. * @param callback The callback to invoke. * @return The return value - * @throws JsiiException if the callback failed. + * @throws JsiiError if the callback failed. */ public JsonNode handleCallback(final Callback callback) { @@ -408,7 +408,7 @@ public JsonNode handleCallback(final Callback callback) { return invokeCallbackSet(callback.getSet()); } - throw new JsiiException("Unrecognized callback type: get/set/invoke"); + throw new JsiiError("Unrecognized callback type: get/set/invoke"); } /** @@ -486,9 +486,9 @@ private Object invokeMethod(final Object obj, final Method method, final Object. throw e; } } catch (InvocationTargetException e) { - throw new JsiiException(e.getTargetException()); + throw new JsiiError(e.getTargetException()); } catch (IllegalAccessException e) { - throw new JsiiException(e); + throw new JsiiError(e); } finally { // revert accessibility. method.setAccessible(accessibility); @@ -503,7 +503,7 @@ private void processCallback(final Callback callback) { try { JsonNode result = handleCallback(callback); this.getClient().completeCallback(callback, null, result); - } catch (JsiiException e) { + } catch (JsiiError e) { this.getClient().completeCallback(callback, e.getMessage(), null); } } @@ -528,7 +528,7 @@ private Method findCallbackMethod(final Class klass, final String signature) return findCallbackMethod(klass.getSuperclass(), signature); } - throw new JsiiException("Unable to find callback method with signature: " + signature); + throw new JsiiError("Unable to find callback method with signature: " + signature); } /** @@ -536,7 +536,7 @@ private Method findCallbackMethod(final Class klass, final String signature) * @param klass is the type on which the getter is to be searched for * @param methodName is the name of the getter method * @return the found Method - * @throws JsiiException if no such method is found + * @throws JsiiError if no such method is found */ private Method findCallbackGetter(final Class klass, final String methodName) { try { @@ -549,7 +549,7 @@ private Method findCallbackGetter(final Class klass, final String methodName) // Ignored! } } - throw new JsiiException(nsme); + throw new JsiiError(nsme); } } @@ -559,7 +559,7 @@ private Method findCallbackGetter(final Class klass, final String methodName) * @param methodName is the name of the setter method * @param valueType is the type of the argument the setter accepts * @return the found Method - * @throws JsiiException if no such method is found + * @throws JsiiError if no such method is found */ private Method findCallbackSetter(final Class klass, final String methodName, final Class valueType) { try { @@ -572,7 +572,7 @@ private Method findCallbackSetter(final Class klass, final String methodName, // Ignored! } } - throw new JsiiException(nsme); + throw new JsiiError(nsme); } } @@ -731,7 +731,7 @@ static Jsii tryGetJsiiAnnotation(final Class type, final boolean inherited) { String loadModuleForClass(Class nativeClass) { final Jsii jsii = tryGetJsiiAnnotation(nativeClass, true); if (jsii == null) { - throw new JsiiException("Unable to find @Jsii annotation for class"); + throw new JsiiError("Unable to find @Jsii annotation for class"); } this.loadModule(jsii.module()); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java similarity index 62% rename from packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java rename to packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java index 80e21beff9..5df347fade 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JSException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java @@ -1,15 +1,15 @@ package software.amazon.jsii; -/* - * Represents an error thrown from the user JS library. +/** + * A nonrecoverable error from the jsii runtime, + * usually the kernel. */ -public final class JSException extends JsiiBaseException { - +public class JsiiError extends JsiiException { /** * Creates an exception. * @param message The error message */ - JSException(final String message) { + JsiiError(final String message) { super(message); } @@ -17,7 +17,7 @@ public final class JSException extends JsiiBaseException { * Creates an exception. * @param e The error that caused this exception */ - JSException(final Throwable e) { + JsiiError(final Throwable e) { super(e); } @@ -26,7 +26,7 @@ public final class JSException extends JsiiBaseException { * @param message The error message * @param e The error that caused this exception */ - JSException(final String message, final Throwable e) { + JsiiError(final String message, final Throwable e) { super(message, e); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index d9ad239218..4704525ecd 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -1,9 +1,25 @@ package software.amazon.jsii; /* - * Represents an irrecoverable error, likely thrown from the kernel. + * An error raised by the jsii runtime. */ -public final class JsiiException extends JsiiBaseException { +public abstract class JsiiException extends RuntimeException { + public static final long serialVersionUID = 1L; + + public static enum Type { + JSII_FAULT("@jsii/kernel.Fault"), + RUNTIME_EXCEPTION("@jsii/kernel.RuntimeException"); + + private final String errorType; + + Type(String str) { + this.errorType = str; + } + + public String toString() { + return this.errorType; + } + } /** * Creates an exception. diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObject.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObject.java index 776b1bbf7a..84761907ec 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObject.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObject.java @@ -53,7 +53,7 @@ protected JsiiObject(final InitializationMode initializationMode) { JsiiObject(@Nullable final JsiiEngine engine, final InitializationMode initializationMode) { this.jsii$engine = JsiiEngine.getEngineFor(this, engine); if (initializationMode != InitializationMode.JSII) { - throw new JsiiException("The only supported initialization mode is '" + InitializationMode.JSII + "'"); + throw new JsiiError("The only supported initialization mode is '" + InitializationMode.JSII + "'"); } } @@ -380,11 +380,11 @@ final T asInterfaceProxy(final Class proxyCl constructor.setAccessible(oldAccessible); } } catch(final NoSuchMethodException nsme) { - throw new JsiiException("Unable to find interface proxy constructor on " + proxyClass.getCanonicalName(), nsme); + throw new JsiiError("Unable to find interface proxy constructor on " + proxyClass.getCanonicalName(), nsme); } catch (final InvocationTargetException | InstantiationException e) { - throw new JsiiException("Unable to initialize interface proxy " + proxyClass.getCanonicalName(), e); + throw new JsiiError("Unable to initialize interface proxy " + proxyClass.getCanonicalName(), e); } catch (final IllegalAccessException iae) { - throw new JsiiException("Unable to invoke constructor of " + proxyClass.getCanonicalName(), iae); + throw new JsiiError("Unable to invoke constructor of " + proxyClass.getCanonicalName(), iae); } } @SuppressWarnings("unchecked") diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectMapper.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectMapper.java index 2dd7d317c8..c5d58a1acc 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectMapper.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectMapper.java @@ -32,7 +32,7 @@ public final class JsiiObjectMapper { /** * Similar to calling JsiiObjectMapper.INSTANCE.treeToValue, but handles a null JsonNode argument - * well, and throws JsiiException instead of JsonProcessingException. + * well, and throws JsiiError instead of JsonProcessingException. * * @param tree the JSON object to parse * @param valueType the expected type value type @@ -228,7 +228,7 @@ private static final class EnumSerializer extends JsonSerializer { public void serialize(final Enum value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException { Jsii jsii = this.tryGetJsiiAnnotation(value.getClass()); if (jsii == null) { - throw new JsiiException("Cannot serialize non-jsii enums"); + throw new JsiiError("Cannot serialize non-jsii enums"); } else { gen.writeStartObject(); gen.writeStringField(TOKEN_ENUM, jsii.fqn() + "/" + value.toString()); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectRef.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectRef.java index 383ead102e..ce218d9872 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectRef.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiObjectRef.java @@ -74,7 +74,7 @@ private JsiiObjectRef(final String objId, final Set interfaces, final Js */ public static JsiiObjectRef parse(final JsonNode objRef) { if (!objRef.has(TOKEN_REF)) { - throw new JsiiException("Malformed object reference. Expecting " + TOKEN_REF); + throw new JsiiError("Malformed object reference. Expecting " + TOKEN_REF); } return new JsiiObjectRef(objRef.get(TOKEN_REF).textValue(), objRef); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 19f277fa99..2dfa2824d1 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -76,7 +76,7 @@ public final class JsiiRuntime { * * @param request The JSON request * @return The JSON response - * @throws JsiiException If the runtime returns an error response. + * @throws RuntimeException If the runtime returns an error response. */ JsonNode requestResponse(final JsonNode request) { try { @@ -104,13 +104,13 @@ JsonNode requestResponse(final JsonNode request) { return resp.get("ok"); } catch (IOException e) { - throw new JsiiException("Unable to send request to jsii-runtime: " + e.toString(), e); + throw new JsiiError("Unable to send request to jsii-runtime: " + e.toString(), e); } } /** * Handles an "error" response by extracting the message and stack trace - * and throwing a JsiiException or a JSException. + * and throwing a JsiiError or a RuntimeException. * * @param resp The response * @return Never @@ -122,11 +122,11 @@ private JsonNode processErrorResponse(final JsonNode resp) { errorMessage += "\n" + resp.get("stack").asText(); } - if (errorName.equals(JsiiException.Type.JS_EXCEPTION.toString())) { - throw new JSException(errorMessage); + if (errorName.equals(JsiiException.Type.RUNTIME_EXCEPTION.toString())) { + throw new RuntimeException(errorMessage); } - throw new JsiiException(errorMessage); + throw new JsiiError(errorMessage); } /** @@ -138,7 +138,7 @@ private JsonNode processErrorResponse(final JsonNode resp) { */ private JsonNode processCallbackResponse(final JsonNode resp) { if (this.callbackHandler == null) { - throw new JsiiException("Cannot process callback since callbackHandler was not set"); + throw new JsiiError("Cannot process callback since callbackHandler was not set"); } Callback callback = JsiiObjectMapper.treeToValue(resp.get("callback"), NativeType.forClass(Callback.class)); @@ -315,7 +315,7 @@ private void handshake() { JsonNode helloResponse = this.readNextResponse(); if (!helloResponse.has("hello")) { - throw new JsiiException("Expecting 'hello' message from jsii-runtime"); + throw new JsiiError("Expecting 'hello' message from jsii-runtime"); } String runtimeVersion = helloResponse.get("hello").asText(); @@ -326,19 +326,19 @@ private void handshake() { * Reads the next response from STDOUT of the child process. * * @return The parsed JSON response. - * @throws JsiiException if we couldn't parse the response. + * @throws JsiiError if we couldn't parse the response. */ JsonNode readNextResponse() { try { String responseLine = this.stdout.readLine(); if (responseLine == null) { - throw new JsiiException("Child process exited unexpectedly!"); + throw new JsiiError("Child process exited unexpectedly!"); } final JsonNode response = JsiiObjectMapper.INSTANCE.readTree(responseLine); JsiiRuntime.notifyInspector(response, MessageInspector.MessageType.Response); return response; } catch (IOException e) { - throw new JsiiException("Unable to read reply from jsii-runtime: " + e.toString(), e); + throw new JsiiError("Unable to read reply from jsii-runtime: " + e.toString(), e); } } @@ -350,7 +350,7 @@ JsonNode readNextResponse() { public JsiiClient getClient() { this.startRuntimeIfNeeded(); if (this.client == null) { - throw new JsiiException("Client not created"); + throw new JsiiError("Client not created"); } return this.client; } @@ -361,13 +361,13 @@ public JsiiClient getClient() { * * @param expectedVersion The version this client expects from the runtime * @param actualVersion The actual version the runtime reports - * @throws JsiiException if versions mismatch + * @throws JsiiError if versions mismatch */ static void assertVersionCompatible(final String expectedVersion, final String actualVersion) { final String shortActualVersion = actualVersion.replaceAll(VERSION_BUILD_PART_REGEX, ""); final String shortExpectedVersion = expectedVersion.replaceAll(VERSION_BUILD_PART_REGEX, ""); if (shortExpectedVersion.compareTo(shortActualVersion) != 0) { - throw new JsiiException("Incompatible jsii-runtime version. Expecting " + throw new JsiiError("Incompatible jsii-runtime version. Expecting " + shortExpectedVersion + ", actual was " + shortActualVersion); } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/UnsafeCast.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/UnsafeCast.java index c1b3a570ea..ccc6a5b9eb 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/UnsafeCast.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/UnsafeCast.java @@ -49,11 +49,11 @@ public static T unsafeCast(final JsiiObject value, constructor.setAccessible(oldAccessible); } } catch (final NoSuchMethodException nsme) { - throw new JsiiException(String.format("Unable to find interface proxy constructor on %s", annotation.value().getCanonicalName()), nsme); + throw new JsiiError(String.format("Unable to find interface proxy constructor on %s", annotation.value().getCanonicalName()), nsme); } catch (final InvocationTargetException | InstantiationException e) { - throw new JsiiException(String.format("Unable to initialize interface proxy %s", annotation.value().getCanonicalName()), e); + throw new JsiiError(String.format("Unable to initialize interface proxy %s", annotation.value().getCanonicalName()), e); } catch (final IllegalAccessException iae) { - throw new JsiiException(String.format("Unable to invoke constructor of %s", annotation.value().getCanonicalName()), iae); + throw new JsiiError(String.format("Unable to invoke constructor of %s", annotation.value().getCanonicalName()), iae); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/Util.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/Util.java index e132ee85aa..7e1c82defe 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/Util.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/Util.java @@ -90,7 +90,7 @@ private static boolean isMatchingGetterPresent(final String getterName, final Cl static String javaPropertyToJSProperty(final Method method) { final String getterSetterMethod = method.getName(); if (!isJavaPropertyMethod(method)) { - throw new JsiiException("Invalid getter/setter method. Must start with get/set"); + throw new JsiiError("Invalid getter/setter method. Must start with get/set"); } String camelCase = getterSetterMethod.substring( @@ -112,7 +112,7 @@ static String javaPropertyToJSProperty(final Method method) { */ static String javaScriptPropertyToJavaPropertyName(final String prefix, final String jsPropertyName) { if (jsPropertyName.isEmpty()) { - throw new JsiiException("jsPropertyName must not be empty"); + throw new JsiiError("jsPropertyName must not be empty"); } StringBuilder sb = new StringBuilder(); diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index a3891b7ff0..9254cf195a 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -17,7 +17,7 @@ import * as tar from './tar-cache'; export enum JsiiErrorType { JSII_FAULT = '@jsii/kernel.Fault', - JS_ERROR = '@jsii/kernel.JSError', + RUNTIME_ERROR = '@jsii/kernel.RuntimeError', } export interface JsiiError extends Error { @@ -31,8 +31,8 @@ export class JsiiFault extends Error implements JsiiError { } } -export class JSError extends Error implements JsiiError { - public readonly name = JsiiErrorType.JS_ERROR; +export class RuntimeError extends Error implements JsiiError { + public readonly name = JsiiErrorType.RUNTIME_ERROR; public constructor(message: string) { super(message); } @@ -1313,7 +1313,7 @@ export class Kernel { // This error can be thrown by the kernel directly, or it can be // thrown from user code. If the error comes from the kernel, then its name field will be populated; // if the error comes from user code, the name field will not be populated. - throw new JSError(e); + throw new RuntimeError(e); } finally { delete this.syncInProgress; } diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 55d3c4be8b..bb77e9c05c 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -1,6 +1,6 @@ import * as fs from 'fs-extra'; -import { JSError, JsiiErrorType, JsiiFault, Kernel } from './kernel'; +import { RuntimeError, JsiiErrorType, JsiiFault, Kernel } from './kernel'; export async function closeRecording(kernel: Kernel): Promise { const logfile: fs.WriteStream = (kernel as any).logfile; @@ -60,8 +60,8 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { return ret; } catch (e: any) { logOutput({ error: e.message, name: e.name }); - if (e.type === JsiiErrorType.JS_ERROR) { - throw new JSError(e.message); + if (e.type === JsiiErrorType.RUNTIME_ERROR) { + throw new RuntimeError(e.message); } throw new JsiiFault(e.message); } From 636516da3bd62c8f4b346f3cf7c704a88b60c1cf Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 19 Sep 2022 13:16:05 -0700 Subject: [PATCH 25/39] comment update --- .../project/src/main/java/software/amazon/jsii/JsiiRuntime.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 2dfa2824d1..d2a9310ca9 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -76,7 +76,7 @@ public final class JsiiRuntime { * * @param request The JSON request * @return The JSON response - * @throws RuntimeException If the runtime returns an error response. + * @throws RuntimeException or JsiiError If the runtime returns an error response. */ JsonNode requestResponse(final JsonNode request) { try { From fa5398e660b70ac94ed9cf22f5b15a50397e09e5 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 20 Sep 2022 14:43:19 -0700 Subject: [PATCH 26/39] review comments --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 7 +++++-- .../src/main/java/software/amazon/jsii/JsiiError.java | 4 +++- .../src/main/java/software/amazon/jsii/JsiiException.java | 2 +- .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 3 ++- packages/@jsii/kernel/src/kernel.ts | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 3d02c262de..ada75214ac 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -334,8 +334,11 @@ public void exceptions() { calc3.add(3); assertEquals(23, calc3.getValue()); boolean thrown = false; - try { calc3.add(10); } - catch (RuntimeException e) { thrown = true; } + try { + calc3.add(10); + } catch (RuntimeException e) { + thrown = true; + } assertTrue(thrown); calc3.setMaxValue(40); calc3.add(10); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java index 5df347fade..f64d3a2d9c 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiError.java @@ -4,7 +4,9 @@ * A nonrecoverable error from the jsii runtime, * usually the kernel. */ -public class JsiiError extends JsiiException { +public final class JsiiError extends JsiiException { + public static final long serialVersionUID = 1L; + /** * Creates an exception. * @param message The error message diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index 4704525ecd..92e5d5a305 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -6,7 +6,7 @@ public abstract class JsiiException extends RuntimeException { public static final long serialVersionUID = 1L; - public static enum Type { + static enum Type { JSII_FAULT("@jsii/kernel.Fault"), RUNTIME_EXCEPTION("@jsii/kernel.RuntimeException"); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index d2a9310ca9..94520bbcbf 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -76,7 +76,8 @@ public final class JsiiRuntime { * * @param request The JSON request * @return The JSON response - * @throws RuntimeException or JsiiError If the runtime returns an error response. + * @throws JsiiError If the runtime returns an error response originating from the @jsii/kernel. + * @throws RuntimeException If the runtime returns an error response. */ JsonNode requestResponse(final JsonNode request) { try { diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 9254cf195a..5100b490c7 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -15,7 +15,7 @@ import * as onExit from './on-exit'; import * as wire from './serialization'; import * as tar from './tar-cache'; -export enum JsiiErrorType { +export const enum JsiiErrorType { JSII_FAULT = '@jsii/kernel.Fault', RUNTIME_ERROR = '@jsii/kernel.RuntimeError', } From 7ccc170b4ee98264952fc3922c297f0b6b44639f Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 21 Sep 2022 13:48:52 -0700 Subject: [PATCH 27/39] use RuntimeError instead of Exception --- .../src/jsii/_kernel/providers/process.py | 2 +- .../@jsii/python-runtime/tests/test_compliance.py | 14 +++++++------- packages/@jsii/python-runtime/tests/test_python.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py index 14466d8dfa..1c1db310ae 100644 --- a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py +++ b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py @@ -326,7 +326,7 @@ def send( elif isinstance(resp, _CallbackResponse): return resp.callback else: - raise JSIIError(resp.error) from JavaScriptError(resp.stack) + raise RuntimeError(resp.error) from JavaScriptError(resp.stack) class ProcessProvider(BaseProvider): diff --git a/packages/@jsii/python-runtime/tests/test_compliance.py b/packages/@jsii/python-runtime/tests/test_compliance.py index 046ae13839..27579ce564 100644 --- a/packages/@jsii/python-runtime/tests/test_compliance.py +++ b/packages/@jsii/python-runtime/tests/test_compliance.py @@ -437,7 +437,7 @@ def test_exceptions(): assert calc3.value == 23 - with pytest.raises(Exception): + with pytest.raises(RuntimeError): calc3.add(10) calc3.max_value = 40 @@ -543,7 +543,7 @@ def override_me(self, mult): obj = ThrowingAsyncVirtualMethods() - with pytest.raises(Exception, match="Thrown by native code"): + with pytest.raises(RuntimeError, match="Thrown by native code"): obj.call_me() @@ -622,7 +622,7 @@ def the_property(self, value): so = ThrowingSyncVirtualMethods() - with pytest.raises(Exception, match="Oh no, this is bad"): + with pytest.raises(RuntimeError, match="Oh no, this is bad"): so.retrieve_value_of_the_property() @@ -638,7 +638,7 @@ def the_property(self, value): so = ThrowingSyncVirtualMethods() - with pytest.raises(Exception, match="Exception from overloaded setter"): + with pytest.raises(RuntimeError, match="Exception from overloaded setter"): so.modify_value_of_the_property("Hii") @@ -703,7 +703,7 @@ def test_fail_syncOverrides_callsDoubleAsync_method(): obj.call_async = True # TODO: Error Handling - with pytest.raises(Exception): + with pytest.raises(RuntimeError): obj.caller_is_method() @@ -712,7 +712,7 @@ def test_fail_syncOverrides_callsDoubleAsync_propertyGetter(): obj.call_async = True # TODO: Error Handling - with pytest.raises(Exception): + with pytest.raises(RuntimeError): obj.caller_is_property @@ -722,7 +722,7 @@ def test_fail_syncOverrides_callsDoubleAsync_propertySetter(): obj.call_async = True # TODO: Error Handling - with pytest.raises(Exception): + with pytest.raises(RuntimeError): obj.caller_is_property = 12 diff --git a/packages/@jsii/python-runtime/tests/test_python.py b/packages/@jsii/python-runtime/tests/test_python.py index 774b3192f9..d05422163a 100644 --- a/packages/@jsii/python-runtime/tests/test_python.py +++ b/packages/@jsii/python-runtime/tests/test_python.py @@ -14,7 +14,7 @@ def test_jsii_error(self): obj = jsii_calc.Calculator() with pytest.raises( - JSIIError, match="Class jsii-calc.Calculator doesn't have a method" + RuntimeError, match="Class jsii-calc.Calculator doesn't have a method" ): jsii.kernel.invoke(obj, "nonexistentMethod") From 0ec3270c6f89eed156f91e237a5d825513a6bbb6 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 21 Sep 2022 14:37:39 -0700 Subject: [PATCH 28/39] need JS error passing... --- .../src/jsii/_kernel/providers/process.py | 11 +++++++---- packages/@jsii/python-runtime/src/jsii/errors.py | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py index 1c1db310ae..60205c441e 100644 --- a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py +++ b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py @@ -62,7 +62,7 @@ CompleteRequest, CompleteResponse, ) -from ...errors import JSIIError, JavaScriptError +from ...errors import ErrorType, JSIIError, JavaScriptError @attr.s(auto_attribs=True, frozen=True, slots=True) @@ -83,10 +83,11 @@ class _OkayResponse: @attr.s(auto_attribs=True, frozen=True, slots=True) -class _ErrorRespose: +class _ErrorResponse: error: str stack: str + name: ErrorType @attr.s(auto_attribs=True, frozen=True, slots=True) @@ -101,7 +102,7 @@ class _CompleteRequest: complete: CompleteRequest -_ProcessResponse = Union[_OkayResponse, _ErrorRespose, _CallbackResponse] +_ProcessResponse = Union[_OkayResponse, _ErrorResponse, _CallbackResponse] def _with_api_key(api_name, asdict): @@ -326,7 +327,9 @@ def send( elif isinstance(resp, _CallbackResponse): return resp.callback else: - raise RuntimeError(resp.error) from JavaScriptError(resp.stack) + if resp.name == ErrorType.RUNTIME_EXCEPTION: + raise RuntimeError(resp.error) from JavaScriptError(resp.stack) + raise JSIIError(resp.error) from JavaScriptError(resp.stack) class ProcessProvider(BaseProvider): diff --git a/packages/@jsii/python-runtime/src/jsii/errors.py b/packages/@jsii/python-runtime/src/jsii/errors.py index 3e85012c88..abd659ebbc 100644 --- a/packages/@jsii/python-runtime/src/jsii/errors.py +++ b/packages/@jsii/python-runtime/src/jsii/errors.py @@ -1,4 +1,5 @@ import textwrap +from enum import Enum class JSIIError(Exception): @@ -11,3 +12,8 @@ def __init__(self, stack): def __str__(self): return "\n" + textwrap.indent(self.stack, " ", lambda line: bool(line)) + + +class ErrorType(Enum): + JSII_FAULT = "@jsii/kernel.Fault" + RUNTIME_ERROR = "@jsii/kernel.RuntimeError" From 8ada6090bf461c1ba65fa65b319821b6785227b8 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 21 Sep 2022 15:23:36 -0700 Subject: [PATCH 29/39] tmp --- .../python-runtime/src/jsii/_kernel/providers/process.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py index 60205c441e..d6db170d7f 100644 --- a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py +++ b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py @@ -87,7 +87,7 @@ class _ErrorResponse: error: str stack: str - name: ErrorType + name: str @attr.s(auto_attribs=True, frozen=True, slots=True) @@ -327,7 +327,7 @@ def send( elif isinstance(resp, _CallbackResponse): return resp.callback else: - if resp.name == ErrorType.RUNTIME_EXCEPTION: + if resp.name == ErrorType.RUNTIME_ERROR: raise RuntimeError(resp.error) from JavaScriptError(resp.stack) raise JSIIError(resp.error) from JavaScriptError(resp.stack) From 220d90393b33647bd31d09291d35a485703702f8 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 21 Sep 2022 17:18:37 -0700 Subject: [PATCH 30/39] some py tests are failing...not clear why yet --- .../python-runtime/src/jsii/_kernel/providers/process.py | 2 +- packages/@jsii/python-runtime/tests/test_compliance.py | 8 +++++--- packages/@jsii/python-runtime/tests/test_python.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py index d6db170d7f..429f9a70fe 100644 --- a/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py +++ b/packages/@jsii/python-runtime/src/jsii/_kernel/providers/process.py @@ -327,7 +327,7 @@ def send( elif isinstance(resp, _CallbackResponse): return resp.callback else: - if resp.name == ErrorType.RUNTIME_ERROR: + if resp.name == ErrorType.RUNTIME_ERROR.value: raise RuntimeError(resp.error) from JavaScriptError(resp.stack) raise JSIIError(resp.error) from JavaScriptError(resp.stack) diff --git a/packages/@jsii/python-runtime/tests/test_compliance.py b/packages/@jsii/python-runtime/tests/test_compliance.py index 27579ce564..9e335d8e22 100644 --- a/packages/@jsii/python-runtime/tests/test_compliance.py +++ b/packages/@jsii/python-runtime/tests/test_compliance.py @@ -87,6 +87,8 @@ from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number from scope.jsii_calc_lib.custom_submodule_name import IReflectable, ReflectableEntry +from src.jsii.errors import JSIIError + # Note: The names of these test functions have been chosen to map as closely to the # Java Compliance tests as possible. # Note: While we could write more expressive and better tests using the functionality @@ -638,7 +640,7 @@ def the_property(self, value): so = ThrowingSyncVirtualMethods() - with pytest.raises(RuntimeError, match="Exception from overloaded setter"): + with pytest.raises(JSIIError, match="Exception from overloaded setter"): so.modify_value_of_the_property("Hii") @@ -703,7 +705,7 @@ def test_fail_syncOverrides_callsDoubleAsync_method(): obj.call_async = True # TODO: Error Handling - with pytest.raises(RuntimeError): + with pytest.raises(JSIIError): obj.caller_is_method() @@ -712,7 +714,7 @@ def test_fail_syncOverrides_callsDoubleAsync_propertyGetter(): obj.call_async = True # TODO: Error Handling - with pytest.raises(RuntimeError): + with pytest.raises(JSIIError): obj.caller_is_property diff --git a/packages/@jsii/python-runtime/tests/test_python.py b/packages/@jsii/python-runtime/tests/test_python.py index d05422163a..774b3192f9 100644 --- a/packages/@jsii/python-runtime/tests/test_python.py +++ b/packages/@jsii/python-runtime/tests/test_python.py @@ -14,7 +14,7 @@ def test_jsii_error(self): obj = jsii_calc.Calculator() with pytest.raises( - RuntimeError, match="Class jsii-calc.Calculator doesn't have a method" + JSIIError, match="Class jsii-calc.Calculator doesn't have a method" ): jsii.kernel.invoke(obj, "nonexistentMethod") From acd0a55f0bca25a6f5b342636b6f960fbaf7f89e Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Thu, 22 Sep 2022 13:58:30 -0700 Subject: [PATCH 31/39] changed the type of some exceptions from JsiiFault to RuntimeError in --- packages/@jsii/kernel/src/kernel.ts | 10 ++++++++-- packages/@jsii/python-runtime/tests/test_compliance.py | 6 +++--- packages/@jsii/runtime/lib/host.ts | 5 +++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 5100b490c7..f14bd52b10 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -482,9 +482,15 @@ export class Kernel { try { result = await promise; this._debug('promise result:', result); - } catch (e) { + } catch (e: any) { this._debug('promise error:', e); - throw new JsiiFault((e as any).message); + if (e.name === JsiiErrorType.JSII_FAULT) { + throw new JsiiFault(e.message); + } + + // default to RuntimeError, since non-kernel errors will not + // have their `name` field defined + throw new RuntimeError(e); } return { diff --git a/packages/@jsii/python-runtime/tests/test_compliance.py b/packages/@jsii/python-runtime/tests/test_compliance.py index 9e335d8e22..2cdad380a9 100644 --- a/packages/@jsii/python-runtime/tests/test_compliance.py +++ b/packages/@jsii/python-runtime/tests/test_compliance.py @@ -640,7 +640,7 @@ def the_property(self, value): so = ThrowingSyncVirtualMethods() - with pytest.raises(JSIIError, match="Exception from overloaded setter"): + with pytest.raises(RuntimeError, match="Exception from overloaded setter"): so.modify_value_of_the_property("Hii") @@ -705,7 +705,7 @@ def test_fail_syncOverrides_callsDoubleAsync_method(): obj.call_async = True # TODO: Error Handling - with pytest.raises(JSIIError): + with pytest.raises(RuntimeError): obj.caller_is_method() @@ -714,7 +714,7 @@ def test_fail_syncOverrides_callsDoubleAsync_propertyGetter(): obj.call_async = True # TODO: Error Handling - with pytest.raises(JSIIError): + with pytest.raises(RuntimeError): obj.caller_is_property diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index e12d33f418..0e1f5befbc 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,4 @@ -import { api, Kernel, JsiiFault, JsiiError } from '@jsii/kernel'; +import { api, Kernel, JsiiFault, JsiiError, RuntimeError } from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -59,7 +59,8 @@ export class KernelHost { completeReq.complete.cbid === callback.cbid ) { if (completeReq.complete.err) { - throw new JsiiFault(completeReq.complete.err); + // TODO: add name to the api so we can use it to decide which type to throw here? + throw new RuntimeError(completeReq.complete.err); } return completeReq.complete.result; From 2dee5e2a53addcaa140416b03bcc1bba5694f9fc Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Fri, 23 Sep 2022 17:57:14 -0700 Subject: [PATCH 32/39] fixed Java bugs. Leaving print statements to investigate final bug in Java later --- .../amazon/jsii/testing/ComplianceTest.java | 9 +++++++++ .../java/software/amazon/jsii/JsiiEngine.java | 13 ++++++++++-- .../software/amazon/jsii/JsiiException.java | 2 +- .../software/amazon/jsii/JsiiRuntime.java | 20 ++++++++++++++++++- packages/@jsii/kernel/src/api.ts | 1 + packages/@jsii/kernel/src/kernel.ts | 4 +++- packages/@jsii/kernel/src/recording.ts | 1 + packages/@jsii/runtime/lib/host.ts | 20 +++++++++++++++++-- 8 files changed, 63 insertions(+), 7 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index ada75214ac..41c33c2091 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -337,6 +337,7 @@ public void exceptions() { try { calc3.add(10); } catch (RuntimeException e) { + assertEquals(RuntimeException.class, e.getClass()); thrown = true; } assertTrue(thrown); @@ -453,6 +454,10 @@ public java.lang.Number overrideMe(java.lang.Number mult) { try { obj.callMe(); } catch (RuntimeException e) { + assertEquals(RuntimeException.class, e.getClass()); + System.err.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHH"); + System.err.println(e.getMessage()); + System.err.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHH"); assertTrue(e.getMessage().contains( "Thrown by native code")); thrown = true; } @@ -522,6 +527,7 @@ public String getTheProperty() { try { so.retrieveValueOfTheProperty(); } catch (RuntimeException e) { + assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains("Oh no, this is bad")); thrown = true; } @@ -540,6 +546,7 @@ public void setTheProperty(String value) { try { so.modifyValueOfTheProperty("Hii"); } catch (RuntimeException e) { + assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains("Exception from overloaded setter")); thrown = true; } @@ -598,6 +605,7 @@ public void setReadWriteString(String value) { assertEquals("Hello", interact.writeAndRead("Hello")); } + /* @Test public void syncOverrides_callsSuper() { SyncOverrides obj = new SyncOverrides(); @@ -632,6 +640,7 @@ public void fail_syncOverrides_callsDoubleAsync_propertyGetter() { obj.getCallerIsProperty(); }); } + */ @Test public void fail_syncOverrides_callsDoubleAsync_propertySetter() { diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index c4222013b4..c5b85541c6 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -486,7 +486,16 @@ private Object invokeMethod(final Object obj, final Method method, final Object. throw e; } } catch (InvocationTargetException e) { - throw new JsiiError(e.getTargetException()); + System.err.println("in invokeMethod()"); + System.err.println("caught InvocationTargetException e with target class: " + e.getTargetException().getClass()); + System.err.println("caught InvocationTargetException e with message: " + e.getCause().getMessage()); + System.err.println("caught InvocationTargetException e with target message: " + e.getTargetException().getMessage()); + System.err.println("rethrowing as runtime error"); + if (e.getTargetException().getClass().equals(JsiiError.class)) { + throw new JsiiError(e.getTargetException()); + } else { + throw new RuntimeException(e.getTargetException()); + } } catch (IllegalAccessException e) { throw new JsiiError(e); } finally { @@ -503,7 +512,7 @@ private void processCallback(final Callback callback) { try { JsonNode result = handleCallback(callback); this.getClient().completeCallback(callback, null, result); - } catch (JsiiError e) { + } catch (Exception e) { this.getClient().completeCallback(callback, e.getMessage(), null); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java index 92e5d5a305..011e7f94d6 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiException.java @@ -8,7 +8,7 @@ public abstract class JsiiException extends RuntimeException { static enum Type { JSII_FAULT("@jsii/kernel.Fault"), - RUNTIME_EXCEPTION("@jsii/kernel.RuntimeException"); + RUNTIME_ERROR("@jsii/kernel.RuntimeError"); private final String errorType; diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 94520bbcbf..36c18b68c8 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -123,10 +123,14 @@ private JsonNode processErrorResponse(final JsonNode resp) { errorMessage += "\n" + resp.get("stack").asText(); } - if (errorName.equals(JsiiException.Type.RUNTIME_EXCEPTION.toString())) { + System.err.println("in processErrorResponse()"); + System.err.println("errorName in processResponse is: " + errorName); + if (errorName.equals(JsiiException.Type.RUNTIME_ERROR.toString())) { + System.err.println("processResponse throwing RuntimeException"); throw new RuntimeException(errorMessage); } + System.err.println("processResponse throwing JsiiError"); throw new JsiiError(errorMessage); } @@ -146,6 +150,7 @@ private JsonNode processCallbackResponse(final JsonNode resp) { JsonNode result = null; String error = null; + String name = null; try { result = this.callbackHandler.handleCallback(callback); } catch (Exception e) { @@ -154,12 +159,25 @@ private JsonNode processCallbackResponse(final JsonNode resp) { } else { error = e.getMessage(); } + + String errClass = e.getClass().toString(); + System.err.println("in processCallbackResponse()"); + System.err.println("errClass == " + errClass); + //e.printStackTrace(); + if (errClass.equals("class software.amazon.jsii.JsiiError")) { + name = JsiiException.Type.JSII_FAULT.toString(); + } else { + name = JsiiException.Type.RUNTIME_ERROR.toString(); + } } ObjectNode completeResponse = JsonNodeFactory.instance.objectNode(); completeResponse.put("cbid", callback.getCbid()); if (error != null) { completeResponse.put("err", error); + completeResponse.put("name", name); + System.err.println("java runtime is putting an error field: " + error); + System.err.println("java runtime is putting a name field: " + name); } if (result != null) { completeResponse.set("result", result); diff --git a/packages/@jsii/kernel/src/api.ts b/packages/@jsii/kernel/src/api.ts index b339a4716b..453b8c639e 100644 --- a/packages/@jsii/kernel/src/api.ts +++ b/packages/@jsii/kernel/src/api.ts @@ -217,6 +217,7 @@ export interface CallbacksResponse { export interface CompleteRequest { readonly cbid: string; readonly err?: string; + readonly name?: string; readonly result?: any; } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index f14bd52b10..2c9d0edee3 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -490,6 +490,7 @@ export class Kernel { // default to RuntimeError, since non-kernel errors will not // have their `name` field defined + console.log(`kernel throwing ${e.message}`); throw new RuntimeError(e); } @@ -523,7 +524,7 @@ export class Kernel { } public complete(req: api.CompleteRequest): api.CompleteResponse { - const { cbid, err, result } = req; + const { cbid, err, result /*, name*/ } = req; this._debug('complete', cbid, err, result); @@ -1319,6 +1320,7 @@ export class Kernel { // This error can be thrown by the kernel directly, or it can be // thrown from user code. If the error comes from the kernel, then its name field will be populated; // if the error comes from user code, the name field will not be populated. + console.log(`kernel throwing ${e.message}`); throw new RuntimeError(e); } finally { delete this.syncInProgress; diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index bb77e9c05c..726bc81a65 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -61,6 +61,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { } catch (e: any) { logOutput({ error: e.message, name: e.name }); if (e.type === JsiiErrorType.RUNTIME_ERROR) { + console.log(`record throwing ${e.message}`); throw new RuntimeError(e.message); } throw new JsiiFault(e.message); diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index 0e1f5befbc..de2cf765f9 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -1,4 +1,11 @@ -import { api, Kernel, JsiiFault, JsiiError, RuntimeError } from '@jsii/kernel'; +import { + api, + Kernel, + JsiiFault, + JsiiError, + RuntimeError, + JsiiErrorType, +} from '@jsii/kernel'; import { EventEmitter } from 'events'; import { Input, IInputOutput } from './in-out'; @@ -59,7 +66,16 @@ export class KernelHost { completeReq.complete.cbid === callback.cbid ) { if (completeReq.complete.err) { - // TODO: add name to the api so we can use it to decide which type to throw here? + console.log('-=-===------------------------------------'); + console.log( + `completeReq.complete.err detected with name field ${completeReq.complete.name}`, + ); + console.log('-=-===------------------------------------'); + if (completeReq.complete.name === JsiiErrorType.JSII_FAULT) { + console.log(`host throwing fault: ${completeReq.complete.err}`); + throw new JsiiFault(completeReq.complete.err); + } + console.log(`host throwing runtime err: ${completeReq.complete.err}`); throw new RuntimeError(completeReq.complete.err); } From d8be24a31f50b13b895bbbb4710ad83f798abd34 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 26 Sep 2022 14:38:09 -0700 Subject: [PATCH 33/39] added name to complete response in java and kernel --- .../amazon/jsii/testing/ComplianceTest.java | 2 - .../amazon/jsii/testing/JsiiClientTest.java | 44 +++++++++++++++++-- .../java/software/amazon/jsii/JsiiClient.java | 7 ++- .../java/software/amazon/jsii/JsiiEngine.java | 5 ++- packages/@jsii/kernel/src/kernel.ts | 12 ++++- packages/@jsii/kernel/src/recording.ts | 2 +- 6 files changed, 61 insertions(+), 11 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 41c33c2091..c235c73bf3 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -605,7 +605,6 @@ public void setReadWriteString(String value) { assertEquals("Hello", interact.writeAndRead("Hello")); } - /* @Test public void syncOverrides_callsSuper() { SyncOverrides obj = new SyncOverrides(); @@ -640,7 +639,6 @@ public void fail_syncOverrides_callsDoubleAsync_propertyGetter() { obj.getCallerIsProperty(); }); } - */ @Test public void fail_syncOverrides_callsDoubleAsync_propertySetter() { diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java index cecf0af514..8befda4b7d 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.jsii.JsiiClient; +import software.amazon.jsii.JsiiError; import software.amazon.jsii.JsiiException; import software.amazon.jsii.JsiiObjectMapper; import software.amazon.jsii.JsiiObjectRef; @@ -117,7 +118,7 @@ public void asyncMethodOverrides() { assertEquals(obj.getObjId(), JsiiObjectRef.parse(first.getInvoke().getObjref()).getObjId()); // now complete the callback with some override value - client.completeCallback(first, null, toSandbox(999)); + client.completeCallback(first, null, null, toSandbox(999)); // end the async invocation, but now we expect the value to be different since we override the method. JsonNode result = client.endAsyncMethod(promise); @@ -146,19 +147,56 @@ public void asyncMethodOverridesThrow() { assertEquals(obj.getObjId(), JsiiObjectRef.parse(first.getInvoke().getObjref()).getObjId()); // now complete the callback with an error - client.completeCallback(first, "Hello, Error", null); + client.completeCallback(first, "Hello, Error", null, null); // end the async invocation, but now we expect the value to be different since we override the method. boolean thrown = false; try { client.endAsyncMethod(promise); - } catch (JsiiException e) { + } catch (RuntimeException e) { + assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains("Hello, Error")); thrown = true; } assertTrue(thrown); } + @Test + public void asyncMethodOverridesThrowWithFault() { + JsiiObjectRef obj = client.createObject("jsii-calc.AsyncVirtualMethods", Arrays.asList(), methodOverride("overrideMe", "myCookie"), Arrays.asList()); + + // begin will return a promise + JsiiPromise promise = client.beginAsyncMethod(obj, "callMe", toSandboxArray()); + assertFalse(promise.getPromiseId().isEmpty()); + + // now we expect to see a callback to "overrideMe" in the pending callbacks queue + + List callbacks = client.pendingCallbacks(); + + assertEquals(1, callbacks.size()); + + Callback first = callbacks.get(0); + assertEquals("overrideMe", first.getInvoke().getMethod()); + assertEquals("myCookie", first.getCookie()); + assertEquals(1, first.getInvoke().getArgs().size()); + assertEquals(JsiiObjectMapper.valueToTree(10), first.getInvoke().getArgs().get(0)); + assertEquals(obj.getObjId(), JsiiObjectRef.parse(first.getInvoke().getObjref()).getObjId()); + + // now complete the callback with an error + client.completeCallback(first, "The kernel encountered a fault", "@jsii/kernel.Fault", null); + + // end the async invocation, but now we expect the value to be different since we override the method. + boolean thrown = false; + try { + client.endAsyncMethod(promise); + } catch (JsiiError e) { + assertEquals(JsiiError.class, e.getClass()); + assertTrue(e.getMessage().contains("The kernel encountered a fault")); + thrown = true; + } + assertTrue(thrown); + } + @Test public void syncVirtualMethods() { JsiiObjectRef obj = client.createObject("jsii-calc.SyncVirtualMethods", Arrays.asList(), methodOverride("virtualMethod","myCookie"), Arrays.asList()); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java index 9dbbad425d..894bd15499 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java @@ -16,6 +16,8 @@ import java.util.Collection; import java.util.List; +import javax.print.DocFlavor.STRING; + import static software.amazon.jsii.Util.extractResource; /** @@ -243,12 +245,15 @@ public List pendingCallbacks() { * Completes a callback. * @param callback The callback to complete. * @param error Error information (or null). + * @param name Error type (or null). * @param result Result (or null). */ - public void completeCallback(final Callback callback, final String error, final JsonNode result) { + public void completeCallback(final Callback callback, final String error, final String name, final JsonNode result) { ObjectNode req = makeRequest("complete"); req.put("cbid", callback.getCbid()); req.put("err", error); + req.put("name", name); + ////////////////// PUT NAME HEREEEEEEEEEEEEEEEEEEE req.set("result", result); this.runtime.requestResponse(req); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index c5b85541c6..7b8b1f9376 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -511,9 +511,10 @@ private Object invokeMethod(final Object obj, final Method method, final Object. private void processCallback(final Callback callback) { try { JsonNode result = handleCallback(callback); - this.getClient().completeCallback(callback, null, result); + this.getClient().completeCallback(callback, null, null, result); } catch (Exception e) { - this.getClient().completeCallback(callback, e.getMessage(), null); + System.err.println("JsiiEngine processing callback with Exception " + e.getMessage() + " of type " + e.getClass()); + this.getClient().completeCallback(callback, e.getMessage(), null, null); } } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 2c9d0edee3..95ea230fd3 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -524,7 +524,7 @@ export class Kernel { } public complete(req: api.CompleteRequest): api.CompleteResponse { - const { cbid, err, result /*, name*/ } = req; + const { cbid, err, result, name } = req; this._debug('complete', cbid, err, result); @@ -535,7 +535,14 @@ export class Kernel { if (err) { this._debug('completed with error:', err); - cb.fail(new Error(err)); + console.error( + `KERNEL found err in complete() with name ${name} and message ${err}`, + ); + cb.fail( + name === JsiiErrorType.JSII_FAULT + ? new JsiiFault(err) + : new RuntimeError(err), + ); } else { const sandoxResult = this._toSandbox( result, @@ -1315,6 +1322,7 @@ export class Kernel { return fn(); } catch (e: any) { if (e.name === JsiiErrorType.JSII_FAULT) { + console.log(`kernel faulting ${e.message}`); throw new JsiiFault(e); } // This error can be thrown by the kernel directly, or it can be diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index 726bc81a65..b2868772dc 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -60,7 +60,7 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { return ret; } catch (e: any) { logOutput({ error: e.message, name: e.name }); - if (e.type === JsiiErrorType.RUNTIME_ERROR) { + if (e.name === JsiiErrorType.RUNTIME_ERROR) { console.log(`record throwing ${e.message}`); throw new RuntimeError(e.message); } From 1da9987c9ac9798d2925f2417731cefa87df4c43 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 26 Sep 2022 14:58:49 -0700 Subject: [PATCH 34/39] logging removed --- .../amazon/jsii/testing/ComplianceTest.java | 3 --- .../java/software/amazon/jsii/JsiiClient.java | 1 - .../java/software/amazon/jsii/JsiiEngine.java | 6 ------ .../java/software/amazon/jsii/JsiiRuntime.java | 16 ++-------------- packages/@jsii/kernel/src/kernel.ts | 6 ------ .../python-runtime/tests/test_compliance.py | 2 -- packages/@jsii/runtime/lib/host.ts | 7 ------- 7 files changed, 2 insertions(+), 39 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index c235c73bf3..3509d79cdc 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -455,9 +455,6 @@ public java.lang.Number overrideMe(java.lang.Number mult) { obj.callMe(); } catch (RuntimeException e) { assertEquals(RuntimeException.class, e.getClass()); - System.err.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHH"); - System.err.println(e.getMessage()); - System.err.println("AHHHHHHHHHHHHHHHHHHHHHHHHHHH"); assertTrue(e.getMessage().contains( "Thrown by native code")); thrown = true; } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java index 894bd15499..bd7e006780 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java @@ -253,7 +253,6 @@ public void completeCallback(final Callback callback, final String error, final req.put("cbid", callback.getCbid()); req.put("err", error); req.put("name", name); - ////////////////// PUT NAME HEREEEEEEEEEEEEEEEEEEE req.set("result", result); this.runtime.requestResponse(req); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index 7b8b1f9376..02ab87ebf8 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -486,11 +486,6 @@ private Object invokeMethod(final Object obj, final Method method, final Object. throw e; } } catch (InvocationTargetException e) { - System.err.println("in invokeMethod()"); - System.err.println("caught InvocationTargetException e with target class: " + e.getTargetException().getClass()); - System.err.println("caught InvocationTargetException e with message: " + e.getCause().getMessage()); - System.err.println("caught InvocationTargetException e with target message: " + e.getTargetException().getMessage()); - System.err.println("rethrowing as runtime error"); if (e.getTargetException().getClass().equals(JsiiError.class)) { throw new JsiiError(e.getTargetException()); } else { @@ -513,7 +508,6 @@ private void processCallback(final Callback callback) { JsonNode result = handleCallback(callback); this.getClient().completeCallback(callback, null, null, result); } catch (Exception e) { - System.err.println("JsiiEngine processing callback with Exception " + e.getMessage() + " of type " + e.getClass()); this.getClient().completeCallback(callback, e.getMessage(), null, null); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 36c18b68c8..3f2e598fb1 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -123,14 +123,10 @@ private JsonNode processErrorResponse(final JsonNode resp) { errorMessage += "\n" + resp.get("stack").asText(); } - System.err.println("in processErrorResponse()"); - System.err.println("errorName in processResponse is: " + errorName); if (errorName.equals(JsiiException.Type.RUNTIME_ERROR.toString())) { - System.err.println("processResponse throwing RuntimeException"); throw new RuntimeException(errorMessage); } - System.err.println("processResponse throwing JsiiError"); throw new JsiiError(errorMessage); } @@ -161,14 +157,8 @@ private JsonNode processCallbackResponse(final JsonNode resp) { } String errClass = e.getClass().toString(); - System.err.println("in processCallbackResponse()"); - System.err.println("errClass == " + errClass); - //e.printStackTrace(); - if (errClass.equals("class software.amazon.jsii.JsiiError")) { - name = JsiiException.Type.JSII_FAULT.toString(); - } else { - name = JsiiException.Type.RUNTIME_ERROR.toString(); - } + name = errClass.equals("class software.amazon.jsii.JsiiError") + ? JsiiException.Type.JSII_FAULT.toString() : JsiiException.Type.RUNTIME_ERROR.toString(); } ObjectNode completeResponse = JsonNodeFactory.instance.objectNode(); @@ -176,8 +166,6 @@ private JsonNode processCallbackResponse(final JsonNode resp) { if (error != null) { completeResponse.put("err", error); completeResponse.put("name", name); - System.err.println("java runtime is putting an error field: " + error); - System.err.println("java runtime is putting a name field: " + name); } if (result != null) { completeResponse.set("result", result); diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 95ea230fd3..9f6dabbf45 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -490,7 +490,6 @@ export class Kernel { // default to RuntimeError, since non-kernel errors will not // have their `name` field defined - console.log(`kernel throwing ${e.message}`); throw new RuntimeError(e); } @@ -535,9 +534,6 @@ export class Kernel { if (err) { this._debug('completed with error:', err); - console.error( - `KERNEL found err in complete() with name ${name} and message ${err}`, - ); cb.fail( name === JsiiErrorType.JSII_FAULT ? new JsiiFault(err) @@ -1322,13 +1318,11 @@ export class Kernel { return fn(); } catch (e: any) { if (e.name === JsiiErrorType.JSII_FAULT) { - console.log(`kernel faulting ${e.message}`); throw new JsiiFault(e); } // This error can be thrown by the kernel directly, or it can be // thrown from user code. If the error comes from the kernel, then its name field will be populated; // if the error comes from user code, the name field will not be populated. - console.log(`kernel throwing ${e.message}`); throw new RuntimeError(e); } finally { delete this.syncInProgress; diff --git a/packages/@jsii/python-runtime/tests/test_compliance.py b/packages/@jsii/python-runtime/tests/test_compliance.py index 2cdad380a9..27579ce564 100644 --- a/packages/@jsii/python-runtime/tests/test_compliance.py +++ b/packages/@jsii/python-runtime/tests/test_compliance.py @@ -87,8 +87,6 @@ from scope.jsii_calc_lib import IFriendly, EnumFromScopedModule, Number from scope.jsii_calc_lib.custom_submodule_name import IReflectable, ReflectableEntry -from src.jsii.errors import JSIIError - # Note: The names of these test functions have been chosen to map as closely to the # Java Compliance tests as possible. # Note: While we could write more expressive and better tests using the functionality diff --git a/packages/@jsii/runtime/lib/host.ts b/packages/@jsii/runtime/lib/host.ts index de2cf765f9..aa0b1dff2c 100644 --- a/packages/@jsii/runtime/lib/host.ts +++ b/packages/@jsii/runtime/lib/host.ts @@ -66,16 +66,9 @@ export class KernelHost { completeReq.complete.cbid === callback.cbid ) { if (completeReq.complete.err) { - console.log('-=-===------------------------------------'); - console.log( - `completeReq.complete.err detected with name field ${completeReq.complete.name}`, - ); - console.log('-=-===------------------------------------'); if (completeReq.complete.name === JsiiErrorType.JSII_FAULT) { - console.log(`host throwing fault: ${completeReq.complete.err}`); throw new JsiiFault(completeReq.complete.err); } - console.log(`host throwing runtime err: ${completeReq.complete.err}`); throw new RuntimeError(completeReq.complete.err); } From 6d3a0979ef8967859aa873ecfdc444c5b7013700 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 26 Sep 2022 15:01:32 -0700 Subject: [PATCH 35/39] remove last print --- packages/@jsii/kernel/src/recording.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/@jsii/kernel/src/recording.ts b/packages/@jsii/kernel/src/recording.ts index b2868772dc..4af9604ab2 100644 --- a/packages/@jsii/kernel/src/recording.ts +++ b/packages/@jsii/kernel/src/recording.ts @@ -61,7 +61,6 @@ export function recordInteraction(kernel: Kernel, inputOutputLogPath: string) { } catch (e: any) { logOutput({ error: e.message, name: e.name }); if (e.name === JsiiErrorType.RUNTIME_ERROR) { - console.log(`record throwing ${e.message}`); throw new RuntimeError(e.message); } throw new JsiiFault(e.message); From 699f025bf6fe5ad72b17aa0a655be9079bc3d32d Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 26 Sep 2022 15:02:29 -0700 Subject: [PATCH 36/39] change formatting of ternary operator in JavaRuntime --- .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 3f2e598fb1..58ad7ec6c0 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -158,7 +158,8 @@ private JsonNode processCallbackResponse(final JsonNode resp) { String errClass = e.getClass().toString(); name = errClass.equals("class software.amazon.jsii.JsiiError") - ? JsiiException.Type.JSII_FAULT.toString() : JsiiException.Type.RUNTIME_ERROR.toString(); + ? JsiiException.Type.JSII_FAULT.toString() + : JsiiException.Type.RUNTIME_ERROR.toString(); } ObjectNode completeResponse = JsonNodeFactory.instance.objectNode(); From 6d825a0664d7c1955825130affbed03587430c05 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 26 Sep 2022 15:03:26 -0700 Subject: [PATCH 37/39] remove old import --- .../project/src/main/java/software/amazon/jsii/JsiiClient.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java index bd7e006780..0f5981d6a1 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiClient.java @@ -16,8 +16,6 @@ import java.util.Collection; import java.util.List; -import javax.print.DocFlavor.STRING; - import static software.amazon.jsii.Util.extractResource; /** From 20857e2084455ca561a6015a60f7efdabcdc5e5e Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 27 Sep 2022 10:55:46 -0700 Subject: [PATCH 38/39] review comments --- .../amazon/jsii/testing/ComplianceTest.java | 15 ++++++++++----- .../amazon/jsii/testing/JsiiClientTest.java | 8 +++----- .../java/software/amazon/jsii/JsiiEngine.java | 13 ++++++++++--- .../java/software/amazon/jsii/JsiiRuntime.java | 5 ++--- packages/@jsii/kernel/src/kernel.ts | 2 +- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 3509d79cdc..52034a5019 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -336,9 +336,11 @@ public void exceptions() { boolean thrown = false; try { calc3.add(10); + } catch (JsiiException e) { + // We expect a RuntimeException that is NOT a JsiiException. + throw e; } catch (RuntimeException e) { - assertEquals(RuntimeException.class, e.getClass()); - thrown = true; + thrown = true; } assertTrue(thrown); calc3.setMaxValue(40); @@ -453,8 +455,9 @@ public java.lang.Number overrideMe(java.lang.Number mult) { boolean thrown = false; try { obj.callMe(); + } catch (JsiiException e) { + throw e; } catch (RuntimeException e) { - assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains( "Thrown by native code")); thrown = true; } @@ -523,8 +526,9 @@ public String getTheProperty() { boolean thrown = false; try { so.retrieveValueOfTheProperty(); + } catch (JsiiException e) { + throw e; } catch (RuntimeException e) { - assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains("Oh no, this is bad")); thrown = true; } @@ -542,8 +546,9 @@ public void setTheProperty(String value) { boolean thrown = false; try { so.modifyValueOfTheProperty("Hii"); + } catch (JsiiException e) { + throw e; } catch (RuntimeException e) { - assertEquals(RuntimeException.class, e.getClass()); assertTrue(e.getMessage().contains("Exception from overloaded setter")); thrown = true; } diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java index 8befda4b7d..0715378209 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/JsiiClientTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.jsii.JsiiClient; -import software.amazon.jsii.JsiiError; import software.amazon.jsii.JsiiException; import software.amazon.jsii.JsiiObjectMapper; import software.amazon.jsii.JsiiObjectRef; @@ -183,15 +182,14 @@ public void asyncMethodOverridesThrowWithFault() { assertEquals(obj.getObjId(), JsiiObjectRef.parse(first.getInvoke().getObjref()).getObjId()); // now complete the callback with an error - client.completeCallback(first, "The kernel encountered a fault", "@jsii/kernel.Fault", null); + client.completeCallback(first, "Hello, Fault", "@jsii/kernel.Fault", null); // end the async invocation, but now we expect the value to be different since we override the method. boolean thrown = false; try { client.endAsyncMethod(promise); - } catch (JsiiError e) { - assertEquals(JsiiError.class, e.getClass()); - assertTrue(e.getMessage().contains("The kernel encountered a fault")); + } catch (JsiiException e) { + assertTrue(e.getMessage().contains("Hello, Fault")); thrown = true; } assertTrue(thrown); diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index 02ab87ebf8..a3d42c1a78 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -486,9 +486,13 @@ private Object invokeMethod(final Object obj, final Method method, final Object. throw e; } } catch (InvocationTargetException e) { - if (e.getTargetException().getClass().equals(JsiiError.class)) { - throw new JsiiError(e.getTargetException()); + if (e.getTargetException() instanceof JsiiError){ + throw (JsiiError)(e.getTargetException()); + } else if (e.getTargetException() instanceof RuntimeException) { + // can rethrow without wrapping here + throw (RuntimeException)(e.getTargetException()); } else { + // Can't just throw a checked error without wrapping it :( throw new RuntimeException(e.getTargetException()); } } catch (IllegalAccessException e) { @@ -508,7 +512,10 @@ private void processCallback(final Callback callback) { JsonNode result = handleCallback(callback); this.getClient().completeCallback(callback, null, null, result); } catch (Exception e) { - this.getClient().completeCallback(callback, e.getMessage(), null, null); + String name = e instanceof JsiiException + ? JsiiException.Type.JSII_FAULT.toString() + : JsiiException.Type.RUNTIME_ERROR.toString(); + this.getClient().completeCallback(callback, e.getMessage(), name, null); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index 58ad7ec6c0..bf73f45ad3 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -156,9 +156,8 @@ private JsonNode processCallbackResponse(final JsonNode resp) { error = e.getMessage(); } - String errClass = e.getClass().toString(); - name = errClass.equals("class software.amazon.jsii.JsiiError") - ? JsiiException.Type.JSII_FAULT.toString() + name = e instanceof JsiiError + ? JsiiException.Type.JSII_FAULT.toString() : JsiiException.Type.RUNTIME_ERROR.toString(); } diff --git a/packages/@jsii/kernel/src/kernel.ts b/packages/@jsii/kernel/src/kernel.ts index 9f6dabbf45..9758780703 100644 --- a/packages/@jsii/kernel/src/kernel.ts +++ b/packages/@jsii/kernel/src/kernel.ts @@ -488,7 +488,7 @@ export class Kernel { throw new JsiiFault(e.message); } - // default to RuntimeError, since non-kernel errors will not + // default to RuntimeError, since non-kernel errors may not // have their `name` field defined throw new RuntimeError(e); } From bf39336b222d864818dbadb295cbebbf913e318e Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 27 Sep 2022 10:59:24 -0700 Subject: [PATCH 39/39] formatting --- .../java/software/amazon/jsii/testing/ComplianceTest.java | 6 +++--- .../src/main/java/software/amazon/jsii/JsiiEngine.java | 4 ++-- .../src/main/java/software/amazon/jsii/JsiiRuntime.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java index 52034a5019..133a548bb0 100644 --- a/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java +++ b/packages/@jsii/java-runtime-test/project/src/test/java/software/amazon/jsii/testing/ComplianceTest.java @@ -335,10 +335,10 @@ public void exceptions() { assertEquals(23, calc3.getValue()); boolean thrown = false; try { - calc3.add(10); + calc3.add(10); } catch (JsiiException e) { - // We expect a RuntimeException that is NOT a JsiiException. - throw e; + // We expect a RuntimeException that is NOT a JsiiException. + throw e; } catch (RuntimeException e) { thrown = true; } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java index a3d42c1a78..8be4fc2a46 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiEngine.java @@ -513,8 +513,8 @@ private void processCallback(final Callback callback) { this.getClient().completeCallback(callback, null, null, result); } catch (Exception e) { String name = e instanceof JsiiException - ? JsiiException.Type.JSII_FAULT.toString() - : JsiiException.Type.RUNTIME_ERROR.toString(); + ? JsiiException.Type.JSII_FAULT.toString() + : JsiiException.Type.RUNTIME_ERROR.toString(); this.getClient().completeCallback(callback, e.getMessage(), name, null); } } diff --git a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java index bf73f45ad3..57f97fcc35 100644 --- a/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java +++ b/packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java @@ -157,8 +157,8 @@ private JsonNode processCallbackResponse(final JsonNode resp) { } name = e instanceof JsiiError - ? JsiiException.Type.JSII_FAULT.toString() - : JsiiException.Type.RUNTIME_ERROR.toString(); + ? JsiiException.Type.JSII_FAULT.toString() + : JsiiException.Type.RUNTIME_ERROR.toString(); } ObjectNode completeResponse = JsonNodeFactory.instance.objectNode();