Better error handling

Jens Pelzetter 2020-07-24 17:14:38 +02:00
parent b58517802d
commit 0b440d649e
2 changed files with 136 additions and 88 deletions

View File

@ -110,7 +110,7 @@ export interface ApiResponse {
text(): Promise<string>;
}
export interface ApiError {
export class ApiError extends Error {
/**
* The HTTP status code reported by the API. `-1` if no status is available.
*/
@ -122,15 +122,25 @@ export interface ApiError {
/**
* The HTTP method used for the failed request.
*/
method: "get" | "post" | "put" | "delete" | "head" | "option";
/**
* A error message providing (additional) details about the error.
*/
message: string;
method: "get" | "post" | "put" | "delete" | "head" | "options";
/**
* The URL used in the failed request.
*/
url: string;
constructor(
status: number,
statusText: string,
method: "get" | "post" | "put" | "delete" | "head" | "options",
message: string,
url: string
) {
super(message);
this.status = status;
this.statusText = statusText;
this.method = method;
this.url = url;
}
}
/**
@ -246,22 +256,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
throw new ApiError(
response.status,
response.statusText,
"get",
"API responded with an error",
url
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
throw new ApiError(
-1,
"n/a",
"get",
"`Failed to execute get: ${err}`",
url
);
}
}
@ -279,22 +289,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
throw new ApiError(
response.status,
response.statusText,
"get",
"API responded with an error",
url
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
throw new ApiError (
-1,
"n/a",
"get",
`Failed to execute get: ${err}`,
url,
};
);
}
}
@ -312,22 +322,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
throw new ApiError(
response.status,
response.statusText,
"get",
"API responded with an error.",
url,
};
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
throw new ApiError (
-1,
"n/a",
"get",
`Failed to execute get: ${err}`,
url,
};
);
}
}
@ -341,22 +351,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
throw new ApiError (
response.status,
response.statusText,
"get",
"API responded with an error.",
url,
};
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
throw new ApiError (
-1,
"n/a",
"get",
`Failed to execute get: ${err}`,
url,
};
);
}
}
@ -367,22 +377,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
throw new ApiError (
response.status,
response.statusText,
"get",
"API responded with an error.",
url,
};
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
throw new ApiError (
-1,
"n/a",
"get",
`Failed to execute get: ${err}`,
url,
};
);
}
}
@ -396,22 +406,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
throw new ApiError (
response.status,
response.statusText,
"get",
"API responded with an error.",
url,
};
);
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
throw new ApiError(
-1,
"n/a",
"get",
`Failed to execute get: ${err}`,
url,
};
);
}
}
}
@ -536,7 +546,15 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse);
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(
new ApiError(
-1,
"n/a",
"get",
`Failed to do GET: ${error}`,
url
)
));
request.end();
});
@ -575,7 +593,13 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(new ApiError(
-1,
"n/a",
"post",
`Failed to do POST: ${error}`,
url
)));
request.write(body);
@ -616,7 +640,13 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(new ApiError(
-1,
"n/a",
"put",
`Failed to do PUT: ${error}`,
url
)));
request.write(body);
@ -656,7 +686,13 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(new ApiError(
-1,
"n/a",
"delete",
`Failed to do DELETE: ${error}`,
url
)));
request.end();
});
@ -696,7 +732,13 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse);
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(new ApiError(
-1,
"n/a",
"head",
`Failed to do HEAD: ${error}`,
url
)));
request.end();
});
@ -736,7 +778,13 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
resolve(apiResponse);
});
});
request.on("error", (error) => reject(error));
request.on("error", (error) => reject(new ApiError(
-1,
"n/a",
"options",
`Failed to do OPTIONS: ${error}`,
url
)));
request.end();
});

View File

@ -10,7 +10,7 @@ import {
} from "./ApiClient";
export * from "./entities";
export { LibreCcmApiClient, ApiResponse, ApiError, buildUrl };
export { LibreCcmApiClient, ApiResponse, ApiError as ApiError, buildUrl };
/**
* Build an client for the LibreCCM RESTful API suitable for use in