Skip to content

Commit 1e08f05

Browse files
committed
Improve share api logging for social content
1 parent 0d82566 commit 1e08f05

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

netlify/edge-functions/common/share-api.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { APIDefinition } from "./definition.ts";
1+
import { APIDefinitions } from "./definition.ts";
22

33
type APIProject = {
44
owner: { handle: string };
@@ -50,6 +50,11 @@ type APIRelease = {
5050
version: string;
5151
};
5252

53+
async function error(url: string, resp: Response): Promise<Error> {
54+
const body = await resp.text();
55+
return Error(`GET ${url}: ${resp.statusText} | ${body}`);
56+
}
57+
5358
function apiHandle(handle: string): string {
5459
return handle.replace("@", "");
5560
}
@@ -64,24 +69,26 @@ const ShareAPI = {
6469
},
6570

6671
getUser: async (handle: string): Promise<APIUser> => {
67-
return fetch(`${ShareAPI.baseURL}/users/${apiHandle(handle)}`).then(
68-
(resp) => {
69-
if (!resp.ok) {
70-
throw new Error(resp.statusText);
71-
}
72+
const url = `${ShareAPI.baseURL}/users/${apiHandle(handle)}`;
7273

73-
return resp.json() as Promise<APIUser>;
74+
return fetch(url).then(async (resp) => {
75+
if (!resp.ok) {
76+
throw await error(url, resp);
7477
}
75-
);
78+
79+
return resp.json() as Promise<APIUser>;
80+
});
7681
},
7782

7883
getProject: async (
7984
handle: string,
8085
projectSlug: string
8186
): Promise<APIProject> => {
82-
return fetch(ShareAPI.projectBaseUrl(handle, projectSlug)).then((resp) => {
87+
const url = ShareAPI.projectBaseUrl(handle, projectSlug);
88+
89+
return fetch(url).then(async (resp) => {
8390
if (!resp.ok) {
84-
throw new Error(resp.statusText);
91+
throw await error(url, resp);
8592
}
8693

8794
return resp.json() as Promise<APIProject>;
@@ -93,15 +100,14 @@ const ShareAPI = {
93100
projectSlug: string,
94101
contribRef: number
95102
): Promise<APIContribution> => {
96-
return fetch(
97-
ShareAPI.projectBaseUrl(
98-
handle,
99-
projectSlug,
100-
`/contributions/${contribRef}`
101-
)
102-
).then((resp) => {
103+
const url = ShareAPI.projectBaseUrl(
104+
handle,
105+
projectSlug,
106+
`/contributions/${contribRef}`
107+
);
108+
return fetch(url).then(async (resp) => {
103109
if (!resp.ok) {
104-
throw new Error(resp.statusText);
110+
throw await error(url, resp);
105111
}
106112

107113
return resp.json() as Promise<APIContribution>;
@@ -113,11 +119,14 @@ const ShareAPI = {
113119
projectSlug: string,
114120
ticketRef: number
115121
): Promise<APITicket> => {
116-
return fetch(
117-
ShareAPI.projectBaseUrl(handle, projectSlug, `/tickets/${ticketRef}`)
118-
).then((resp) => {
122+
const url = ShareAPI.projectBaseUrl(
123+
handle,
124+
projectSlug,
125+
`/tickets/${ticketRef}`
126+
);
127+
return fetch(url).then(async (resp) => {
119128
if (!resp.ok) {
120-
throw new Error(resp.statusText);
129+
throw await error(url, resp);
121130
}
122131

123132
return resp.json() as Promise<APITicket>;
@@ -129,11 +138,15 @@ const ShareAPI = {
129138
projectSlug: string,
130139
version: string
131140
): Promise<APIRelease> => {
132-
return fetch(
133-
ShareAPI.projectBaseUrl(handle, projectSlug, `/releases/${version}`)
134-
).then((resp) => {
141+
const url = ShareAPI.projectBaseUrl(
142+
handle,
143+
projectSlug,
144+
`/releases/${version}`
145+
);
146+
147+
return fetch(url).then(async (resp) => {
135148
if (!resp.ok) {
136-
throw new Error(resp.statusText);
149+
throw await error(url, resp);
137150
}
138151

139152
return resp.json() as Promise<APIRelease>;
@@ -159,9 +172,9 @@ const ShareAPI = {
159172
url = mkUrl(branchRef);
160173
}
161174

162-
return fetch(url).then((resp) => {
175+
return fetch(url).then(async (resp) => {
163176
if (!resp.ok) {
164-
throw new Error(resp.statusText);
177+
throw await error(url, resp);
165178
}
166179

167180
return resp.json() as Promise<APIDefinitions>;

0 commit comments

Comments
 (0)