Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for attachments #244

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Nylas Java SDK Changelog

### Unreleased

### Added
* Added new error class `NylasSdkRemoteClosedError` for when the remote connection is closed

### [2.4.1] - Released 2024-07-26

### Changed
Expand Down
79 changes: 29 additions & 50 deletions src/main/kotlin/com/nylas/NylasClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import okhttp3.Response
import java.io.IOException
import java.lang.Exception
import java.lang.reflect.Type
import java.net.SocketException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -76,112 +77,84 @@ class NylasClient(
* Access the Applications API
* @return The Applications API
*/
fun applications(): Applications {
return Applications(this)
}
fun applications(): Applications = Applications(this)

/**
* Access the Attachments API
* @return The Attachments API
*/
fun attachments(): Attachments {
return Attachments(this)
}
fun attachments(): Attachments = Attachments(this)

/**
* Access the Auth API
* @return The Auth API
*/
fun auth(): Auth {
return Auth(this)
}
fun auth(): Auth = Auth(this)

/**
* Access the Calendars API
* @return The Calendars API
*/
fun calendars(): Calendars {
return Calendars(this)
}
fun calendars(): Calendars = Calendars(this)

/**
* Access the Connectors API
* @return The Connectors API
*/
fun connectors(): Connectors {
return Connectors(this)
}
fun connectors(): Connectors = Connectors(this)

/**
* Access the Drafts API
* @return The Drafts API
*/
fun drafts(): Drafts {
return Drafts(this)
}
fun drafts(): Drafts = Drafts(this)

/**
* Access the Events API
* @return The Events API
*/
fun events(): Events {
return Events(this)
}
fun events(): Events = Events(this)

/**
* Access the Folders API
* @return The Folders API
*/
fun folders(): Folders {
return Folders(this)
}
fun folders(): Folders = Folders(this)

/**
* Access the Grants API
* @return The Grants API
*/
fun grants(): Grants {
return Grants(this)
}
fun grants(): Grants = Grants(this)

/**
* Access the Messages API
* @return The Messages API
*/
fun messages(): Messages {
return Messages(this)
}
fun messages(): Messages = Messages(this)

/**
* Access the Threads API
* @return The Threads API
*/
fun threads(): Threads {
return Threads(this)
}
fun threads(): Threads = Threads(this)

/**
* Access the Webhooks API
* @return The Webhooks API
*/
fun webhooks(): Webhooks {
return Webhooks(this)
}
fun webhooks(): Webhooks = Webhooks(this)

/**
* Access the Contacts API
* @return The Contacts API
*/
fun contacts(): Contacts {
return Contacts(this)
}
fun contacts(): Contacts = Contacts(this)

/**
* Get a URL builder instance for the Nylas API.
*/
fun newUrlBuilder(): HttpUrl.Builder {
return apiUri.newBuilder()
}
fun newUrlBuilder(): HttpUrl.Builder = apiUri.newBuilder()

/**
* Execute a GET request to the Nylas API.
Expand Down Expand Up @@ -393,6 +366,14 @@ class NylasClient(
return response.body() ?: throw Exception("Unexpected null response body")
} catch (e: SocketTimeoutException) {
throw NylasSdkTimeoutError(finalUrl.toString(), httpClient.callTimeoutMillis())
} catch (e: SocketException) {
throw NylasSdkRemoteClosedError(finalUrl.toString(), e.message ?: "Unknown error")
} catch (e: Exception) {
throw NylasApiError(
type = "unknown",
message = "Unknown error occurred: ${e.message}",
statusCode = 0,
)
}
}

Expand Down Expand Up @@ -542,13 +523,11 @@ class NylasClient(
*/
companion object {
val DEFAULT_BASE_URL = Region.US.nylasApiUrl
private fun defaultHttpClient(): OkHttpClient.Builder {
return OkHttpClient.Builder()
.connectTimeout(90, TimeUnit.SECONDS)
.readTimeout(90, TimeUnit.SECONDS)
.writeTimeout(90, TimeUnit.SECONDS)
.protocols(listOf(Protocol.HTTP_1_1))
.addNetworkInterceptor(HttpLoggingInterceptor())
}
private fun defaultHttpClient(): OkHttpClient.Builder = OkHttpClient.Builder()
.connectTimeout(90, TimeUnit.SECONDS)
.readTimeout(90, TimeUnit.SECONDS)
.writeTimeout(90, TimeUnit.SECONDS)
.protocols(listOf(Protocol.HTTP_1_1))
.addNetworkInterceptor(HttpLoggingInterceptor())
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/com/nylas/models/NylasSdkRemoteClosedError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nylas.models

/**
* Error thrown when the Nylas API closes the connection before the Nylas SDK receives a response.
* @param url The URL that timed out.
* @param originalErrorMessage The error message from the library that closed the connection.
*/
class NylasSdkRemoteClosedError(
url: String,
originalErrorMessage: String,
) : AbstractNylasSdkError("Nylas API closed the connection before the Nylas SDK received a response.")
Loading