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

Provide better error reporting #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 13 additions & 8 deletions src/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function postResource<T>(
query: string,
headers: StringMap,
data: Record<string, unknown>,
): Promise<T | undefined> {
): Promise<T | Error> {
// TODO(@qu4k): add test resource
try {
const response = await apiFetch(`${ENDPOINT}${query}`, {
Expand All @@ -20,11 +20,16 @@ export async function postResource<T>(
body: JSON.stringify(data),
});

if (!response || !response.ok) return undefined;
if (!response.ok) {
return new Error(
`The server respond with ${response.status} ${response.statusText}:\n` +
await response.text(),
);
}
const value = await response.json();
return value as T;
} catch {
return undefined;
} catch (e) {
return e;
}
}

Expand All @@ -50,8 +55,8 @@ export interface PublishModule extends Record<string, unknown> {
export async function postPublishModule(
key: string,
module: PublishModule,
): Promise<PublishResponse | undefined> {
const response: PublishResponse | undefined = await postResource(
): Promise<PublishResponse | Error> {
const response: PublishResponse | Error = await postResource(
"/api/publish",
{ "Authorization": key },
module,
Expand All @@ -67,8 +72,8 @@ export interface PiecesResponse {
export async function postPieces(
uploadToken: string,
pieces: StringMap,
): Promise<PiecesResponse | undefined> {
const response: PiecesResponse | undefined = await postResource(
): Promise<PiecesResponse | Error> {
const response: PiecesResponse | Error = await postResource(
"/api/piece",
{ "X-UploadToken": uploadToken },
{
Expand Down
16 changes: 9 additions & 7 deletions src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,23 +361,25 @@ export async function publish(options: Options, name?: string) {
}

const uploadResponse = await postPublishModule(apiKey, module);
if (!uploadResponse) {
// TODO(@qu4k): provide better error reporting
throw new Error("Something broke when publishing... ");
if (uploadResponse instanceof Error) {
throw new Error(
`Something broke when publishing; ${uploadResponse.message}`,
);
}

const pieceResponse = await postPieces(
uploadResponse.token,
Object.entries(matchedContent).reduce((prev, [key, value]) => {
prev[key.substr(1)] = value;
prev[key.substring(1)] = value;
return prev;
}, {} as Record<string, string>),
);
// TODO(@oganexon): same, needs consistency

if (!pieceResponse) {
// TODO(@qu4k): provide better error reporting
throw new Error("Something broke when sending pieces... ");
if (pieceResponse instanceof Error) {
throw new Error(
`Something broke when sending pieces; ${pieceResponse.message}`,
);
}

const configPath = await defaultConfig();
Expand Down