3

My code that I have looks like this:

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();
        }
    }


} 

What should I do with the this. references that were in my JavaScript source before I started to try and convert it?

I don't know how to code this part in Typescript:

    AuthenticationRetrievalException = (message) => {
        this.name = 'AuthenticationRetrieval';
        this.message = message;
    }
wonea
  • 4,783
  • 17
  • 86
  • 139

2 Answers2

2

If I understand you well and you want to trow an error with custom message, you can use class Error like this:

throw new Error("Authentication not found");

But I am not sure if this is what you want to do.

David Bohunek
  • 3,181
  • 1
  • 21
  • 26
2

I highly recommend you not creating your own Error classes in JavaScript(or TypeScript) and just use Error (ref : How do I create a custom Error in JavaScript?)

But here is how you can do it in TypeScript. At the root level of your file (not inside your class):

function AuthenticationRetrievalError (message) {
    this.name = "AuthenticationRetrievalError";
    this.message = (message || "");
}
AuthenticationRetrievalError.prototype = Error.prototype;

Then then from inside your class :

throw new AuthenticationRetrievalError('foo');
Community
  • 1
  • 1
basarat
  • 261,912
  • 58
  • 460
  • 511
  • If you are suggesting using Error then that is the way I think may be best to go. I was using the above code as I found that on the web and just reused. Can you suggest to me in your answer a way that I could use Error instead of my own classes. –  Jul 29 '14 at 10:40