admin管理员组文章数量:1326282
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;
}
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;
}
Share
Improve this question
edited Feb 19, 2018 at 12:07
wonea
4,96917 gold badges91 silver badges143 bronze badges
asked Jul 29, 2014 at 10:15
user3568783user3568783
2
- What exactly would you like to do with the message? – David Bohunek Commented Jul 29, 2014 at 10:32
- @DavidBohunek - At this time I would just like to thow the exception so it's caught and I can then return a false from isAuthenticated – user3568783 Commented Jul 29, 2014 at 10:39
2 Answers
Reset to default 2If 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.
I highly remend 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');
本文标签: javascriptHow can I handle an exception message with TypeScriptStack Overflow
版权声明:本文标题:javascript - How can I handle an exception message with TypeScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742212442a2433960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论