Skip to content

Commit ff38989

Browse files
committed
more backward compatible error response, but still useful message
1 parent cc4663c commit ff38989

3 files changed

Lines changed: 48 additions & 24 deletions

File tree

src/main/kotlin/com/nylas/models/CreateEventRequest.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,21 @@ data class CreateEventRequest(
235235
/**
236236
* Builds the [Timespan] object.
237237
* @return [Timespan] object.
238-
* @throws IllegalArgumentException if endTime is not after startTime.
238+
* @throws NylasApiError if endTime is not after startTime.
239239
*/
240240
fun build(): Timespan {
241241
// Validate that endTime must be after startTime
242-
require(endTime > startTime) {
243-
"Invalid Timespan: endTime ($endTime) must be after startTime ($startTime). " +
244-
"Timespan events require a positive duration. " +
245-
"For point-in-time events, use CreateEventRequest.When.Time instead."
242+
if (endTime <= startTime) {
243+
throw NylasApiError(
244+
type = "invalid_request_error",
245+
message = "Invalid request: Timespan events require endTime to be after startTime",
246+
statusCode = 400,
247+
requestId = null,
248+
providerError = null,
249+
validationErrors = mapOf(
250+
"when.end_time" to "End time ($endTime) must be after start time ($startTime). For point-in-time events, use CreateEventRequest.When.Time instead."
251+
)
252+
)
246253
}
247254
return Timespan(startTime, endTime, startTimezone, endTimezone)
248255
}

src/main/kotlin/com/nylas/models/UpdateEventRequest.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,24 @@ data class UpdateEventRequest(
277277
/**
278278
* Builds the [Timespan] object.
279279
* @return [Timespan] object.
280-
* @throws IllegalArgumentException if both startTime and endTime are provided and endTime is not after startTime.
280+
* @throws NylasApiError if both startTime and endTime are provided and endTime is not after startTime.
281281
*/
282282
fun build(): Timespan {
283283
// Validate that if both times are set, endTime must be after startTime
284284
// Local variables for smart-cast null safety
285285
val start = startTime
286286
val end = endTime
287-
if (start != null && end != null) {
288-
require(end > start) {
289-
"Invalid Timespan: endTime ($end) must be after startTime ($start). " +
290-
"Timespan events require a positive duration. " +
291-
"For point-in-time events, use UpdateEventRequest.When.Time instead."
292-
}
287+
if (start != null && end != null && end <= start) {
288+
throw NylasApiError(
289+
type = "invalid_request_error",
290+
message = "Invalid request: Timespan events require endTime to be after startTime",
291+
statusCode = 400,
292+
requestId = null,
293+
providerError = null,
294+
validationErrors = mapOf(
295+
"when.end_time" to "End time ($end) must be after start time ($start). For point-in-time events, use UpdateEventRequest.When.Time instead."
296+
)
297+
)
293298
}
294299
return Timespan(startTime, endTime, startTimezone, endTimezone)
295300
}

src/test/kotlin/com/nylas/resources/EventsTests.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -511,28 +511,34 @@ class EventsTests {
511511

512512
@Test
513513
fun `UpdateEventRequest Timespan with zero duration throws exception`() {
514-
val exception = org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
514+
val exception = org.junit.jupiter.api.assertThrows<NylasApiError> {
515515
UpdateEventRequest.When.Timespan.Builder()
516516
.startTime(1620000000)
517517
.endTime(1620000000)
518518
.build()
519519
}
520520

521-
assert(exception.message!!.contains("endTime (1620000000) must be after startTime (1620000000)"))
522-
assert(exception.message!!.contains("For point-in-time events, use UpdateEventRequest.When.Time instead"))
521+
assertEquals(400, exception.statusCode)
522+
assert(exception.message.contains("endTime to be after startTime"))
523+
assert(exception.validationErrors != null)
524+
assert(exception.validationErrors!!["when.end_time"]!!.contains("End time (1620000000) must be after start time (1620000000)"))
525+
assert(exception.validationErrors!!["when.end_time"]!!.contains("For point-in-time events, use UpdateEventRequest.When.Time instead"))
523526
}
524527

525528
@Test
526529
fun `UpdateEventRequest Timespan with negative duration throws exception`() {
527-
val exception = org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
530+
val exception = org.junit.jupiter.api.assertThrows<NylasApiError> {
528531
UpdateEventRequest.When.Timespan.Builder()
529532
.startTime(1620003600)
530533
.endTime(1620000000)
531534
.build()
532535
}
533536

534-
assert(exception.message!!.contains("endTime (1620000000) must be after startTime (1620003600)"))
535-
assert(exception.message!!.contains("Timespan events require a positive duration"))
537+
assertEquals(400, exception.statusCode)
538+
assert(exception.message.contains("endTime to be after startTime"))
539+
assert(exception.validationErrors != null)
540+
assert(exception.validationErrors!!["when.end_time"]!!.contains("End time (1620000000) must be after start time (1620003600)"))
541+
assert(exception.validationErrors!!["when.end_time"]!!.contains("For point-in-time events, use UpdateEventRequest.When.Time instead"))
536542
}
537543

538544
@Test
@@ -586,24 +592,30 @@ class EventsTests {
586592

587593
@Test
588594
fun `CreateEventRequest Timespan with zero duration throws exception`() {
589-
val exception = org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
595+
val exception = org.junit.jupiter.api.assertThrows<NylasApiError> {
590596
CreateEventRequest.When.Timespan.Builder(1620000000, 1620000000)
591597
.build()
592598
}
593599

594-
assert(exception.message!!.contains("endTime (1620000000) must be after startTime (1620000000)"))
595-
assert(exception.message!!.contains("For point-in-time events, use CreateEventRequest.When.Time instead"))
600+
assertEquals(400, exception.statusCode)
601+
assert(exception.message.contains("endTime to be after startTime"))
602+
assert(exception.validationErrors != null)
603+
assert(exception.validationErrors!!["when.end_time"]!!.contains("End time (1620000000) must be after start time (1620000000)"))
604+
assert(exception.validationErrors!!["when.end_time"]!!.contains("For point-in-time events, use CreateEventRequest.When.Time instead"))
596605
}
597606

598607
@Test
599608
fun `CreateEventRequest Timespan with negative duration throws exception`() {
600-
val exception = org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
609+
val exception = org.junit.jupiter.api.assertThrows<NylasApiError> {
601610
CreateEventRequest.When.Timespan.Builder(1620003600, 1620000000)
602611
.build()
603612
}
604613

605-
assert(exception.message!!.contains("endTime (1620000000) must be after startTime (1620003600)"))
606-
assert(exception.message!!.contains("Timespan events require a positive duration"))
614+
assertEquals(400, exception.statusCode)
615+
assert(exception.message.contains("endTime to be after startTime"))
616+
assert(exception.validationErrors != null)
617+
assert(exception.validationErrors!!["when.end_time"]!!.contains("End time (1620000000) must be after start time (1620003600)"))
618+
assert(exception.validationErrors!!["when.end_time"]!!.contains("For point-in-time events, use CreateEventRequest.When.Time instead"))
607619
}
608620

609621
@Test

0 commit comments

Comments
 (0)