Javascript 如何使用TypeScript处理异常消息?

Javascript 如何使用TypeScript处理异常消息?,javascript,typescript,Javascript,Typescript,我的代码如下所示: class UserService implements IUserService { data: IUserServiceData = { expirationDate: null, isAuthenticated: false, }; static $inject = []; constructor () {} isAuthenticated = () => { if (

我的代码如下所示:

class UserService implements IUserService {

    data: IUserServiceData = {
        expirationDate: null,
        isAuthenticated: false,
    };

    static $inject = [];

    constructor () {}

    isAuthenticated = () => {
        if (this.data.isAuthenticated && !this.isAuthenticationExpired(this.data.expirationDate)) {
            return true;
        } else {
            try {
                this.retrieveSavedData();
            } catch (e) {
                return false;
                // throw new NoAuthenticationException('Authentication not found');
            }
            return true;
        }
    };

    // Original Javascript code here
    //function AuthenticationRetrievalException(message) {
    //    this.name = 'AuthenticationRetrieval';
    //   this.message = message;
    //}

    AuthenticationRetrievalException = (message) => {
        this.name = 'AuthenticationRetrieval';
        this.message = message;
    }

    retrieveSavedData = () => {
        var savedData = this.utilityService.userCache.get('data');
        if (typeof savedData === 'undefined') {
            throw new AuthenticationRetrievalException('No authentication data exists');
        } else if (isAuthenticationExpired(savedData.expirationDate)) {
            throw new AuthenticationExpiredException('Authentication token has already expired');
        } else {
            this.data = savedData;
            this.setHttpAuthHeader();
        }
    }


} 
throw new Error("Authentication not found");
我该怎么处理这个。在我开始尝试转换JavaScript源代码之前,JavaScript源代码中存在的引用

我不知道如何用Typescript编写这部分代码:

    AuthenticationRetrievalException = (message) => {
        this.name = 'AuthenticationRetrieval';
        this.message = message;
    }

如果我很了解您,并且您希望在自定义消息中出现错误,您可以像这样使用class
error

class UserService implements IUserService {

    data: IUserServiceData = {
        expirationDate: null,
        isAuthenticated: false,
    };

    static $inject = [];

    constructor () {}

    isAuthenticated = () => {
        if (this.data.isAuthenticated && !this.isAuthenticationExpired(this.data.expirationDate)) {
            return true;
        } else {
            try {
                this.retrieveSavedData();
            } catch (e) {
                return false;
                // throw new NoAuthenticationException('Authentication not found');
            }
            return true;
        }
    };

    // Original Javascript code here
    //function AuthenticationRetrievalException(message) {
    //    this.name = 'AuthenticationRetrieval';
    //   this.message = message;
    //}

    AuthenticationRetrievalException = (message) => {
        this.name = 'AuthenticationRetrieval';
        this.message = message;
    }

    retrieveSavedData = () => {
        var savedData = this.utilityService.userCache.get('data');
        if (typeof savedData === 'undefined') {
            throw new AuthenticationRetrievalException('No authentication data exists');
        } else if (isAuthenticationExpired(savedData.expirationDate)) {
            throw new AuthenticationExpiredException('Authentication token has already expired');
        } else {
            this.data = savedData;
            this.setHttpAuthHeader();
        }
    }


} 
throw new Error("Authentication not found");

但是我不确定这是否是您想要做的。

如果我很了解您,并且您希望在自定义消息中出现错误,您可以像这样使用class
error

class UserService implements IUserService {

    data: IUserServiceData = {
        expirationDate: null,
        isAuthenticated: false,
    };

    static $inject = [];

    constructor () {}

    isAuthenticated = () => {
        if (this.data.isAuthenticated && !this.isAuthenticationExpired(this.data.expirationDate)) {
            return true;
        } else {
            try {
                this.retrieveSavedData();
            } catch (e) {
                return false;
                // throw new NoAuthenticationException('Authentication not found');
            }
            return true;
        }
    };

    // Original Javascript code here
    //function AuthenticationRetrievalException(message) {
    //    this.name = 'AuthenticationRetrieval';
    //   this.message = message;
    //}

    AuthenticationRetrievalException = (message) => {
        this.name = 'AuthenticationRetrieval';
        this.message = message;
    }

    retrieveSavedData = () => {
        var savedData = this.utilityService.userCache.get('data');
        if (typeof savedData === 'undefined') {
            throw new AuthenticationRetrievalException('No authentication data exists');
        } else if (isAuthenticationExpired(savedData.expirationDate)) {
            throw new AuthenticationExpiredException('Authentication token has already expired');
        } else {
            this.data = savedData;
            this.setHttpAuthHeader();
        }
    }


} 
throw new Error("Authentication not found");

但我不确定这是否是您想要做的。

我强烈建议您不要在JavaScript(或TypeScript)中创建自己的错误类,而只使用
Error
(参考:)

但这里是如何在TypeScript中实现它的。在文件的根级别(不在类内部):

然后在你的课堂上:

throw new AuthenticationRetrievalError('foo');

我强烈建议您不要在JavaScript(或TypeScript)中创建自己的错误类,只需使用
Error
(参考:)

但这里是如何在TypeScript中实现它的。在文件的根级别(不在类内部):

然后在你的课堂上:

throw new AuthenticationRetrievalError('foo');

你到底想对消息做什么?@DavidBohunek-此时,我只想假设异常被捕获,然后我可以从isauthenticated返回false。你到底想对消息做什么?@DavidBohunek-此时,我只想假设异常被捕获,然后我可以返回false如果您建议使用错误,那么我认为这是最好的方式。我正在使用上面的代码,因为我在web上发现了这些代码,并且只是重复使用。你能在你的回答中建议我使用错误而不是我自己的类吗?如果你建议使用错误,那么我认为这是最好的方法。我正在使用上面的代码,因为我在web上发现了这些代码,并且只是重复使用。你能在你的回答中建议我用错误代替我自己的类吗?Basarat和你的建议是一样的,所以我认为这可能是解决这个问题的方法。是的。这也是我的建议。这真的是JavaScript的错。Basarat和你的建议是一样的,所以我认为这可能是解决这个问题的方法。是的。这也是我的建议。这真的是JavaScript的错。