Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 725b3c7

Browse files
committed
Throw exceptions immediately
1 parent a38d44b commit 725b3c7

File tree

3 files changed

+12
-51
lines changed

3 files changed

+12
-51
lines changed

simplekotlinmail-server/src/main/kotlin/net/axay/simplekotlinmail/server/MessageListener.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class MailHandler(
3737
if (fromListener != null) {
3838
val incomingFrom = IncomingFrom(from, context)
3939
fromListener.invoke(incomingFrom)
40-
incomingFrom.throwExceptions()
4140
}
4241
}
4342

@@ -47,15 +46,13 @@ class MailHandler(
4746
if (recipientListener != null) {
4847
val incomingRecipient = IncomingRecipient(from, recipient, recipients, context)
4948
recipientListener.invoke(incomingRecipient)
50-
incomingRecipient.throwExceptions()
5149
}
5250
}
5351

5452
override fun data(data: InputStream): String? {
5553
return if (mailListener != null) {
5654
val incomingMail = IncomingMail(from, recipients, EmailConverter.emlToEmail(data), context)
5755
mailListener.invoke(incomingMail)
58-
incomingMail.throwExceptions()
5956

6057
incomingMail.response
6158
} else null

simplekotlinmail-server/src/main/kotlin/net/axay/simplekotlinmail/server/exchange/IncomingMail.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ class IncomingMail internal constructor(
1515
) : IncomingMailExchange(context) {
1616
internal var response: String? = null
1717

18-
private var ifTooMuchData = false
19-
private var tooMuchDataMessage: String? = null
20-
2118
/**
2219
* Set a custom response success message.
2320
*/
@@ -29,15 +26,6 @@ class IncomingMail internal constructor(
2926
* Reject the message if an input stream provides
3027
* more data than the listener can handle
3128
*/
32-
fun tooMuchData(message: String? = null) {
33-
ifTooMuchData = true
34-
tooMuchDataMessage = message
35-
}
36-
37-
override fun throwSpecificExceptions() {
38-
if (ifTooMuchData)
39-
if (tooMuchDataMessage != null)
40-
throw TooMuchDataException(tooMuchDataMessage)
41-
else throw TooMuchDataException()
42-
}
29+
fun tooMuchData(message: String? = null): Nothing =
30+
if (message != null) throw TooMuchDataException(message) else throw TooMuchDataException()
4331
}

simplekotlinmail-server/src/main/kotlin/net/axay/simplekotlinmail/server/exchange/IncomingMailExchange.kt

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,21 @@ import org.subethamail.smtp.MessageContext
55
import org.subethamail.smtp.RejectException
66

77
abstract class IncomingMailExchange(val context: MessageContext) {
8-
private var ifReject = false
9-
private var rejectMessage: String? = null
10-
private var rejectCode: Int? = null
11-
12-
private var ifDropConnection = false
13-
private var dropConnectionMessage: String? = null
14-
private var dropConnectionCode: Int? = null
15-
168
/**
179
* Reject this message.
1810
*/
19-
fun reject(message: String? = null, statusCode: Int? = null) {
20-
ifReject = true
21-
rejectMessage = message
22-
rejectCode = statusCode
23-
}
11+
fun reject(message: String? = null, statusCode: Int? = null): Nothing =
12+
throw RejectException(
13+
statusCode ?: RejectException.DEFAULT_CODE,
14+
message ?: RejectException.DEFAULT_MESSAGE
15+
)
2416

2517
/**
2618
* Drop the connection.
2719
*/
28-
fun dropConnection(message: String? = null, statusCode: Int? = null) {
29-
ifDropConnection = true
30-
dropConnectionMessage = message
31-
dropConnectionCode = statusCode
32-
}
33-
34-
internal fun throwExceptions() {
35-
if (ifReject)
36-
throw RejectException(
37-
rejectCode ?: RejectException.DEFAULT_CODE,
38-
rejectMessage ?: RejectException.DEFAULT_MESSAGE
39-
)
40-
if (ifDropConnection)
41-
throw DropConnectionException(
42-
dropConnectionCode ?: DropConnectionException.DEFAULT_CODE,
43-
dropConnectionMessage ?: DropConnectionException.DEFAULT_MESSAGE
44-
)
45-
throwSpecificExceptions()
46-
}
47-
48-
protected open fun throwSpecificExceptions() { }
20+
fun dropConnection(message: String? = null, statusCode: Int? = null): Nothing =
21+
throw DropConnectionException(
22+
statusCode ?: DropConnectionException.DEFAULT_CODE,
23+
message ?: DropConnectionException.DEFAULT_MESSAGE
24+
)
4925
}

0 commit comments

Comments
 (0)