Skip to content

Commit

Permalink
Fix: return rejected promise instead throwing an error
Browse files Browse the repository at this point in the history
  • Loading branch information
mtripg6666tdr committed Jan 16, 2023
1 parent 2f49838 commit 05d7ded
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const CONTENT_TYPE = "content-type";
*/
class CandyGetError extends Error {}
const genParamErrMsg = (name:string) => `Invalid Param:${name}`;
const genRejectedPromise = (message:string) => Promise.reject(new CandyGetError(message));

/**
* A function that does nothing
Expand Down Expand Up @@ -151,10 +152,10 @@ function candyget<T extends keyof BodyTypes>(urlOrMethod:Url|HttpMethods, return
body = rawBody || null;
}
// validate params (not strictly)
if(!HttpMethodsSet.includes(method)) throw new CandyGetError(genParamErrMsg("method"));
if(!BodyTypesSet.includes(returnType)) throw new CandyGetError(genParamErrMsg("returnType"));
if(typeof overrideOptions != "object") throw new CandyGetError(genParamErrMsg("options"));
if(!(url instanceof URL)) throw new CandyGetError(genParamErrMsg("url"));
if(!HttpMethodsSet.includes(method)) return genRejectedPromise(genParamErrMsg("method"));
if(!BodyTypesSet.includes(returnType)) return genRejectedPromise(genParamErrMsg("returnType"));
if(typeof overrideOptions != "object") return genRejectedPromise(genParamErrMsg("options"));
if(!(url instanceof URL)) return genRejectedPromise(genParamErrMsg("url"));
// prepare optiosn
const options = Object.assign(createEmpty(), candyget.defaultOptions, overrideOptions);
const headers = Object.assign(createEmpty(), candyget.defaultOptions.headers, overrideOptions.headers);
Expand All @@ -166,8 +167,8 @@ function candyget<T extends keyof BodyTypes>(urlOrMethod:Url|HttpMethods, return
if(typeof body !== "string" && !options.headers[CONTENT_TYPE]){
options.headers[CONTENT_TYPE] = "application/json";
}
if(typeof options.timeout != "number" || options.timeout < 1) throw new CandyGetError(genParamErrMsg("timeout"));
if(typeof options.maxRedirects != "number" || options.maxRedirects < 0) throw new CandyGetError(genParamErrMsg("maxRedirects"));
if(typeof options.timeout != "number" || options.timeout < 1) return genRejectedPromise(genParamErrMsg("timeout"));
if(typeof options.maxRedirects != "number" || options.maxRedirects < 0) return genRejectedPromise(genParamErrMsg("maxRedirects"));
// execute request
let redirectCount = 0;
// store the original url
Expand Down

0 comments on commit 05d7ded

Please sign in to comment.