TypeScript Exceptions

TypeScript exceptions are generated as TypeScript Error type descendants.

Igor:

exception AccessDenied
{
    string user;
    string data_key;
    string reason;
}

TypeScript:

export class AccessDenied extends Error {
    user!: string;
    dataKey!: string;
    reason!: string;

    constructor() {
        super('AccessDenied');
        Object.setPrototypeOf(this, new.target.prototype);
    }
}

See https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work why setting prototype explicitely is required.

Setting Error Message

By default exception class name is used as error message. This behavior can be altered by marking one of string fields with error_message attribute. Then this field value is treated and stored as Error.message.

Igor:

[* json.enabled]
exception AccessDenied
{
    string user;
    string data_key;
    [ts error_message]
    string reason;
}

TypeScript:

export class AccessDenied extends Error {
    user!: string;
    dataKey!: string;

    constructor() {
        super();
        Object.setPrototypeOf(this, new.target.prototype);
    }

    static fromJson(json: Igor.Json.JsonValue): AccessDenied {
        const jsonObject = json as Igor.Json.JsonObject;
        const obj = new AccessDenied();
        obj.user = jsonObject['user'] as string;
        obj.dataKey = jsonObject['data_key'] as string;
        obj.message = jsonObject['reason'] as string;
        return obj;
    }

    static toJson(value: AccessDenied): Igor.Json.JsonValue {
        const result: Igor.Json.JsonObject = {};
        result['user'] = value.user;
        result['data_key'] = value.dataKey;
        result['reason'] = value.message;
        return result;
    }

    toJson(): Igor.Json.JsonValue {
        return AccessDenied.toJson(this);
    }
}

Note, that reason field is not generated anymore. Instead, message is used as a value source for json serialization.