Skip to content

Commit 0150d00

Browse files
authored
display error messages from Posit Cloud error responses (#6591)
1 parent c3b96bd commit 0150d00

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/publish/posit-cloud/api/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Application,
1010
Bundle,
1111
Content,
12+
ErrorBody,
1213
OutputRevision,
1314
Task,
1415
User,
@@ -170,7 +171,19 @@ export class PositCloudClient {
170171
return await response.text() as unknown as T;
171172
}
172173
} else if (response.status >= 400) {
173-
throw new ApiError(response.status, response.statusText);
174+
const json = await response.json() as unknown as ErrorBody;
175+
let errorDescription = undefined;
176+
if (json.error) {
177+
errorDescription = json.error;
178+
if (json.error_type) {
179+
errorDescription = `${errorDescription}, code=${json.error_type}`;
180+
}
181+
}
182+
throw new ApiError(
183+
response.status,
184+
response.statusText,
185+
errorDescription,
186+
);
174187
} else {
175188
throw new Error(`${response.status} - ${response.statusText}`);
176189
}

src/publish/posit-cloud/api/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ export type Task = {
3939
state: string;
4040
error?: string;
4141
};
42+
43+
export type ErrorBody = {
44+
error?: string;
45+
error_type?: string;
46+
};

src/publish/types.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
/*
2-
* types.ts
3-
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
5-
*
6-
*/
2+
* types.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*/
76

87
import { ProjectContext } from "../project/types.ts";
98

109
export class ApiError extends Error {
1110
public constructor(
1211
public readonly status: number,
13-
public readonly statusText: string
12+
public readonly statusText: string,
13+
public readonly description: string | undefined = undefined,
1414
) {
15-
super(`API Error: ${status} - ${statusText}`);
15+
let message = `API Error: ${status} - ${statusText}`;
16+
if (description) {
17+
message = `${message} (${description})`;
18+
}
19+
super(message);
1620
}
1721
}
1822

0 commit comments

Comments
 (0)