Skip to content

Commit eb85f15

Browse files
committed
Update error format
1 parent 2db808b commit eb85f15

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface Credentials {
2626

2727
export function parse(string: string): Credentials | undefined {
2828
if (typeof string !== 'string') {
29-
return undefined;
29+
throw new TypeError('Expected a string');
3030
}
3131

3232
// parse header
@@ -53,28 +53,26 @@ export function parse(string: string): Credentials | undefined {
5353
*/
5454
export function format(credentials: Credentials): string {
5555
if (typeof credentials !== 'object' || credentials === null) {
56-
throw new TypeError('"credentials" must be an object');
56+
throw new TypeError('Expected an object');
5757
}
5858

5959
if (
6060
typeof credentials.name !== 'string' ||
6161
typeof credentials.pass !== 'string'
6262
) {
63-
throw new TypeError(
64-
'"credentials" must have string properties "name" and "pass"',
65-
);
63+
throw new TypeError('Object must have string properties "name" and "pass"');
6664
}
6765

6866
// RFC 7617 disallows colon in username
6967
if (credentials.name.includes(':')) {
70-
throw new TypeError('"name" must not contain a colon');
68+
throw new TypeError('Object "name" must not contain a colon');
7169
}
7270

7371
const str = credentials.name + ':' + credentials.pass;
7472

7573
if (CONTROL_CHARS_REGEXP.test(str)) {
7674
throw new TypeError(
77-
'"name" and "pass" must not contain control characters',
75+
'Object "name" and "pass" must not contain control characters',
7876
);
7977
}
8078

src/parse.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { describe, it, assert } from 'vitest';
22
import { parse } from './index.js';
33

44
describe('parse(string)', function () {
5-
describe('with undefined string', function () {
6-
it('should return undefined', function () {
7-
assert.strictEqual((parse as any)(), undefined);
5+
describe('with non string', function () {
6+
it('should throw', function () {
7+
assert.throws(() => parse(undefined as any), /Expected a string/);
88
});
99
});
1010

0 commit comments

Comments
 (0)