Skip to content

Commit

Permalink
Fix HttpServiceError instance checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Uden committed May 18, 2017
1 parent f8d3bd7 commit f046918
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
10 changes: 10 additions & 0 deletions mangarack-component-core/src/errors/HttpServiceError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ export class HttpServiceError extends Error {
this.body = currentState.body;
this.statusCode = currentState.statusCode;
}

/**
* Determines whether the error is a HttpServiceError.
* @param error The error.
* @return Indicates whether the error is a HttpServiceError.
*/
public static isInstance(error: Error): error is HttpServiceError {
let httpError = error as HttpServiceError;
return httpError && typeof httpError.body !== 'undefined' && typeof httpError.statusCode !== 'undefined';
}
}
19 changes: 8 additions & 11 deletions mangarack-component-core/src/providers/kissmanga/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ function createSeries(document: mio.IHtmlServiceDocument): mio.ISeries {
*/
async function downloadDocumentAsync(address: string): Promise<mio.IHtmlServiceDocument> {
try {
let body = await httpService().text(address, mio.ControlType.TimeoutWithRetry).getAsync();
let body = await httpService().text(address, mio.ControlType.Timeout).getAsync();
return htmlService().load(body);
} catch (error) {
if (error instanceof mio.HttpServiceError) {
let httpServiceError = error as mio.HttpServiceError;
if (httpServiceError.statusCode === 503) {
let document = htmlService().load(httpServiceError.body);
let pass = document('form[id=challenge-form]').find('input[name=pass]').attr('value');
if (pass) {
await mio.promise(callback => setTimeout(callback, 8000));
await httpService().text(`${providerDomain}/cdn-cgi/l/chk_jschl?pass=${pass}`, mio.ControlType.TimeoutWithRetry).getAsync();
return downloadDocumentAsync(address);
}
if (mio.HttpServiceError.isInstance(error) && error.statusCode === 503) {
let document = htmlService().load(error.body);
let pass = document('form[id=challenge-form]').find('input[name=pass]').attr('value');
if (pass) {
await mio.promise(callback => setTimeout(callback, 8000));
await httpService().text(`${providerDomain}/cdn-cgi/l/chk_jschl?pass=${pass}`, mio.ControlType.TimeoutWithRetry).getAsync();
return downloadDocumentAsync(address);
}
}
throw error;
Expand Down
7 changes: 2 additions & 5 deletions mangarack-component-library/src/remote/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ export async function openRemoteLibraryAsync(host: string, password: string): Pr
return undefined;
}
} catch (error) {
if (error instanceof mio.HttpServiceError) {
let httpServiceError = error as mio.HttpServiceError;
if (httpServiceError.statusCode === 401) {
return undefined;
}
if (mio.HttpServiceError.isInstance(error) && error.statusCode === 401) {
return undefined;
}
throw error;
}
Expand Down
7 changes: 2 additions & 5 deletions mangarack-component-library/src/remote/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,8 @@ async function handleNotFound<T>(defaultValue: T, action: () => Promise<T>): Pro
try {
return await action();
} catch (error) {
if (error instanceof mio.HttpServiceError) {
let httpServiceError = error as mio.HttpServiceError;
if (httpServiceError.statusCode === 404) {
return defaultValue;
}
if (mio.HttpServiceError.isInstance(error) && error.statusCode === 404) {
return defaultValue;
}
throw error;
}
Expand Down

0 comments on commit f046918

Please sign in to comment.