Skip to content

Commit

Permalink
chore: simplify error class without status code
Browse files Browse the repository at this point in the history
  • Loading branch information
Benmuiruri committed Feb 17, 2025
1 parent 8c2f5ce commit 0949850
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
13 changes: 6 additions & 7 deletions src/lib/authentication-error.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
export class AuthError extends Error {
constructor(
public status: number,
public errorMessage: string
) {
super(errorMessage);
this.name = 'AuthError';
}

static INVALID_CREDENTIALS() {
return new AuthError(403, 'Invalid username or password');
return new AuthError('Invalid username or password');
}

static MISSING_CREDENTIALS() {
return new AuthError(401, 'Missing username or password');
return new AuthError('Missing username or password');
}

static TOKEN_CREATION_FAILED(username: string, domain: string) {
return new AuthError(401, `Failed to obtain token for ${username} at ${domain}`);
return new AuthError(`Failed to obtain token for ${username} at ${domain}`);
}

static MISSING_FACILITY(username: string) {
return new AuthError(401, `User ${username} does not have a facility_id connected to their user doc`);
return new AuthError(`User ${username} does not have a facility_id connected to their user doc`);
}

static INCOMPATIBLE_CHT_CORE_VERSION(domain: string, chtCoreVersion: string) {
return new AuthError(401, `CHT Core Version must be 4.7.0 or higher. "${domain}" is running ${chtCoreVersion}.`);
return new AuthError(`CHT Core Version must be 4.7.0 or higher. "${domain}" is running ${chtCoreVersion}.`);
}

static CANNOT_PARSE_CHT_VERSION(chtCoreVersion: string, domain: string) {
return new AuthError(401, `Cannot parse cht core version ${chtCoreVersion} for instance "${domain}"`);
return new AuthError(`Cannot parse cht core version ${chtCoreVersion} for instance "${domain}"`);
}
}
14 changes: 2 additions & 12 deletions test/lib/cht-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ describe('lib/cht-session.ts', () => {
});

it('throws AuthError for invalid credentials', async () => {
mockAxios.post.rejects({ response: { status: 401 } });
mockAxios.post.rejects({ response: { status: 403 } });

await expect(ChtSession.default.create(mockAuthInfo, 'user', 'wrong_pwd'))
.to.be.rejectedWith('Invalid username or password')
.and.to.eventually.be.instanceof(AuthError)
.and.to.have.property('status', 401);
.and.to.eventually.be.instanceof(AuthError);
});

it('throw cht yields no authtoken', async () => {
Expand All @@ -92,15 +91,6 @@ describe('lib/cht-session.ts', () => {
mockAxios.get.onSecondCall().resolves({ data: { version: { app: '4.6.5' } } });
await expect(ChtSession.default.create(mockAuthInfo, 'user', 'pwd')).to.eventually.be.rejectedWith('CHT Core Version must be');
});

it('throws if invalid credentials', async () => {
mockAxios.post.rejects({ response: { status: 401 } });

await expect(ChtSession.default.create(mockAuthInfo, 'user', 'wrong_pwd'))
.to.be.rejectedWith('Invalid username or password')
.and.to.eventually.be.instanceof(AuthError)
.and.to.have.property('status', 401);
});
});

it('createFromDataString', async () => {
Expand Down

0 comments on commit 0949850

Please sign in to comment.