Skip to content

Commit

Permalink
refactor createProfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanmar511 committed Nov 27, 2017
1 parent 2cb3f94 commit c5df7a6
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions ts/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,29 @@ export class Retryer {
}
}

/**
* @return profile iff response indicates success and the returned profile was
* valid.
* @throws error when the response indicated failure or the returned profile
* was not valid.
*/
function responseToProfileOrError(
err: Error, body: object, response: http.ServerResponse): RequestProfile {
if (response && isErrorResponseStatusCode(response.statusCode)) {
if (isServerBackoffResponse(response)) {
throw new BackoffResponseError(response);
}
throw new Error(response.statusMessage);
}
if (err) {
throw err;
}
if (isRequestProfile(body)) {
return body;
}
throw new Error(`Profile not valid: ${JSON.stringify(body)}.`);
}

/**
* Polls profiler server for instructions on behalf of a task and
* collects and uploads profiles as requested
Expand Down Expand Up @@ -333,22 +356,15 @@ export class Profiler extends common.ServiceObject {
return new Promise<RequestProfile>((resolve, reject) => {
this.request(
options,
(err: Error, prof: object, response: http.ServerResponse) => {
if (response && isErrorResponseStatusCode(response.statusCode)) {
if (isServerBackoffResponse(response)) {
reject(new BackoffResponseError(response));
}
reject(new Error(response.statusMessage));
}
if (err) {
reject(err);
}
if (isRequestProfile(prof)) {
(err: Error, body: object, response: http.ServerResponse) => {
try {
const prof = responseToProfileOrError(err, body, response);
this.logger.debug(
`Successfully created profile ${prof.profileType}.`);
resolve(prof);
} catch (err) {
reject(err);
}
reject(new Error(`Profile not valid: ${JSON.stringify(prof)}.`));
});
});
}
Expand Down

0 comments on commit c5df7a6

Please # to comment.