Skip to content

Commit 6819a76

Browse files
committed
Fix #5, #16; User email wont assign until server confirms
Prior to this commit the email id was even assigned to the user in case the server was throwing error. This was a bug and is now fixed.
1 parent adcbd38 commit 6819a76

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mvnw.cmd
99
**/application-dev.yml
1010
**/application-prod.yml
1111
**/application-qa.yml
12+
src/main/resources/application.yml
1213
*.db
1314
lib/
1415
bin/

src/main/java/io/github/trashemail/EmailInteraction/EmailServerInteraction.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.github.trashemail.EmailInteraction;
22

3+
import io.github.trashemail.Telegram.TelegramRequestHandler;
4+
import io.github.trashemail.utils.exceptions.EmailAliasNotCreatedExecption;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
37
import org.springframework.beans.factory.annotation.Autowired;
48
import org.springframework.http.*;
59
import org.springframework.stereotype.Component;
@@ -21,8 +25,9 @@ public class EmailServerInteraction {
2125
@Autowired
2226
RestTemplate restTemplate;
2327

24-
25-
public String createEmailId(User user) throws HttpClientErrorException {
28+
private static final Logger log = LoggerFactory.getLogger(EmailServerInteraction.class);
29+
30+
public String createEmailId(User user) throws HttpClientErrorException, EmailAliasNotCreatedExecption {
2631

2732
HttpHeaders headers = new HttpHeaders();
2833
headers.setBasicAuth(
@@ -48,13 +53,8 @@ public String createEmailId(User user) throws HttpClientErrorException {
4853
"successfully Created :)";
4954
}
5055

51-
if(response.getStatusCode() == HttpStatus.BAD_REQUEST &&
52-
((String)response.getBody()).contains("Alias already exists")){
53-
return "Email ID : *"+user.getEmailId()+"* "+
54-
"already taken :)";
55-
}
56-
57-
return response.getBody().toString();
56+
log.error(response.getStatusCode().toString() + response.getBody());
57+
throw new EmailAliasNotCreatedExecption(response.getBody().toString());
5858
}
5959

6060
public boolean deleteEmailId(User user) {

src/main/java/io/github/trashemail/Telegram/TelegramRequestHandler.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.regex.Pattern;
66

77
import io.github.trashemail.Configurations.EmailServerConfiguration;
8+
import io.github.trashemail.utils.exceptions.EmailAliasNotCreatedExecption;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011
import org.springframework.beans.factory.annotation.Autowired;
@@ -26,7 +27,7 @@ public class TelegramRequestHandler {
2627

2728
private static final Logger log = LoggerFactory.getLogger(TelegramRequestHandler.class);
2829

29-
public String createEmail(User user) throws HttpClientErrorException {
30+
public String createEmail(User user) throws HttpClientErrorException, EmailAliasNotCreatedExecption {
3031
return emailServerInteraction.createEmailId(user);
3132
}
3233
public String deleteEmail(User user) throws HttpClientErrorException{
@@ -96,8 +97,29 @@ public String handleRequest(long chatId, String text) {
9697
User user = new User(chatId,
9798
emailId,
9899
emailServerConfiguration.getEmailServerImapTaregtUsername());
99-
userRepository.save(user);
100-
return this.createEmail(user);
100+
101+
String response = null;
102+
try {
103+
104+
response = this.createEmail(user);
105+
106+
}catch (EmailAliasNotCreatedExecption emailAliasNotCreatedExecption){
107+
108+
log.error("Exception " + emailAliasNotCreatedExecption.getMessage());
109+
return "Email address is already taken.\nPlease try something else.";
110+
111+
}catch (HttpClientErrorException httpClientErrorException){
112+
113+
log.error("Exception " + httpClientErrorException.getMessage());
114+
return "Email address is already taken.\nPlease try something else.";
115+
}
116+
117+
if(response!=null) {
118+
userRepository.save(user);
119+
return "Something bad just happened with me. Stay back till I get fixed.";
120+
}
121+
122+
return response;
101123
}
102124
else{
103125
if(argument.isEmpty())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.github.trashemail.utils.exceptions;
2+
3+
public class EmailAliasNotCreatedExecption extends Exception {
4+
public EmailAliasNotCreatedExecption(String exceptionMessage){
5+
super(exceptionMessage);
6+
}
7+
}

0 commit comments

Comments
 (0)