Skip to content

Commit 78a8df4

Browse files
committed
Tweak client error responses
1 parent e20d503 commit 78a8df4

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderRestClientService.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CoderRestClientService {
5050
}
5151
}
5252

53-
class CoderRestClient(var url: URL, private var token: String) {
53+
class CoderRestClient(var url: URL, var token: String) {
5454
private var httpClient: OkHttpClient
5555
private var retroRestClient: CoderV2RestFacade
5656

@@ -75,7 +75,7 @@ class CoderRestClient(var url: URL, private var token: String) {
7575
fun me(): User {
7676
val userResponse = retroRestClient.me().execute()
7777
if (!userResponse.isSuccessful) {
78-
throw AuthenticationResponseException("Could not retrieve information about logged user:${userResponse.code()}, reason: ${userResponse.message().ifBlank { "no reason provided" }}")
78+
throw AuthenticationResponseException("Authentication to $url failed with code ${userResponse.code()}, ${userResponse.message().ifBlank { "has your token expired?" }}")
7979
}
8080

8181
return userResponse.body()!!
@@ -88,7 +88,7 @@ class CoderRestClient(var url: URL, private var token: String) {
8888
fun workspaces(): List<Workspace> {
8989
val workspacesResponse = retroRestClient.workspaces("owner:me").execute()
9090
if (!workspacesResponse.isSuccessful) {
91-
throw WorkspaceResponseException("Could not retrieve Coder Workspaces:${workspacesResponse.code()}, reason: ${workspacesResponse.message().ifBlank { "no reason provided" }}")
91+
throw WorkspaceResponseException("Retrieving workspaces from $url failed with code ${workspacesResponse.code()}, reason: ${workspacesResponse.message().ifBlank { "no reason provided" }}")
9292
}
9393

9494
return workspacesResponse.body()!!.workspaces
@@ -97,15 +97,15 @@ class CoderRestClient(var url: URL, private var token: String) {
9797
fun buildInfo(): BuildInfo {
9898
val buildInfoResponse = retroRestClient.buildInfo().execute()
9999
if (!buildInfoResponse.isSuccessful) {
100-
throw java.lang.IllegalStateException("Could not retrieve build information for Coder instance $url, reason:${buildInfoResponse.message().ifBlank { "no reason provided" }}")
100+
throw java.lang.IllegalStateException("Retrieving build information for $url failed with code ${buildInfoResponse.code()}, reason:${buildInfoResponse.message().ifBlank { "no reason provided" }}")
101101
}
102102
return buildInfoResponse.body()!!
103103
}
104104

105-
fun template(templateID: UUID): Template {
105+
private fun template(templateID: UUID): Template {
106106
val templateResponse = retroRestClient.template(templateID).execute()
107107
if (!templateResponse.isSuccessful) {
108-
throw TemplateResponseException("Failed to retrieve template with id: $templateID, reason: ${templateResponse.message().ifBlank { "no reason provided" }}")
108+
throw TemplateResponseException("Retrieving template with id $templateID from $url failed with code ${templateResponse.code()}, reason: ${templateResponse.message().ifBlank { "no reason provided" }}")
109109
}
110110
return templateResponse.body()!!
111111
}
@@ -114,7 +114,7 @@ class CoderRestClient(var url: URL, private var token: String) {
114114
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.START, null, null, null, null)
115115
val buildResponse = retroRestClient.createWorkspaceBuild(workspaceID, buildRequest).execute()
116116
if (buildResponse.code() != HTTP_CREATED) {
117-
throw WorkspaceResponseException("Failed to build workspace ${workspaceName}: ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
117+
throw WorkspaceResponseException("Building workspace $workspaceName on $url failed with code ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
118118
}
119119

120120
return buildResponse.body()!!
@@ -124,7 +124,7 @@ class CoderRestClient(var url: URL, private var token: String) {
124124
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.STOP, null, null, null, null)
125125
val buildResponse = retroRestClient.createWorkspaceBuild(workspaceID, buildRequest).execute()
126126
if (buildResponse.code() != HTTP_CREATED) {
127-
throw WorkspaceResponseException("Failed to stop workspace ${workspaceName}: ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
127+
throw WorkspaceResponseException("Stopping workspace $workspaceName on $url failed with code ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
128128
}
129129

130130
return buildResponse.body()!!
@@ -136,7 +136,7 @@ class CoderRestClient(var url: URL, private var token: String) {
136136
val buildRequest = CreateWorkspaceBuildRequest(template.activeVersionID, lastWorkspaceTransition, null, null, null, null)
137137
val buildResponse = retroRestClient.createWorkspaceBuild(workspaceID, buildRequest).execute()
138138
if (buildResponse.code() != HTTP_CREATED) {
139-
throw WorkspaceResponseException("Failed to update workspace ${workspaceName}: ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
139+
throw WorkspaceResponseException("Updating workspace $workspaceName on $url failed with code ${buildResponse.code()}, reason: ${buildResponse.message().ifBlank { "no reason provided" }}")
140140
}
141141

142142
return buildResponse.body()!!

0 commit comments

Comments
 (0)