Skip to content

Commit 61f4065

Browse files
committed
added onStatus and onCatch to replace the onError function
1 parent cea6884 commit 61f4065

5 files changed

Lines changed: 121 additions & 27 deletions

File tree

src/errors.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,95 @@
1+
/**
2+
* @deprecated
3+
*/
14
export class UriError extends Error {}
5+
6+
7+
/**
8+
* @deprecated
9+
*/
210
export class QueryError extends Error {}
11+
12+
13+
/**
14+
* @deprecated
15+
*/
316
export class HeaderError extends Error {}
17+
18+
19+
/**
20+
* @deprecated
21+
*/
422
export class CustomError extends Error {}
523

24+
/**
25+
* @deprecated
26+
*/
627
export class UriUnknownVersionError extends UriError {
728
constructor(version: string) {
829
super(`Unable to find version '${version}'`)
930
}
1031
}
1132

33+
/**
34+
* @deprecated
35+
*/
1236
export class UriUnexpectedError extends CustomError {
1337
constructor(readonly child: Error) {
1438
super()
1539
this.child = child
1640
}
1741
}
1842

43+
/**
44+
* @deprecated
45+
*/
1946
export class QueryUnknownVersionError extends UriError {
2047
constructor(version: string) {
2148
super(`Unable to find version '${version}'`)
2249
}
2350
}
2451

52+
/**
53+
* @deprecated
54+
*/
2555
export class QueryUnexpectedError extends CustomError {
2656
constructor(readonly child: Error) {
2757
super()
2858
this.child = child
2959
}
3060
}
3161

62+
/**
63+
* @deprecated
64+
*/
3265
export class HeaderWrongPrefixError extends HeaderError {
3366
constructor(expected: string, actual: string) {
3467
super(`'${actual}' does not start with the expected prefix '${expected}'`)
3568
}
3669
}
3770

71+
/**
72+
* @deprecated
73+
*/
3874
export class HeaderUnknownVersionError extends HeaderError {
3975
constructor(version: string) {
4076
super(`Unable to find version '${version}'`)
4177
}
4278
}
4379

80+
/**
81+
* @deprecated
82+
*/
4483
export class HeaderUnexpectedError extends HeaderError {
4584
constructor(readonly child: Error) {
4685
super()
4786
this.child = child
4887
}
4988
}
5089

90+
/**
91+
* @deprecated
92+
*/
5193
export class CustomUnexpectedError extends CustomError {
5294
constructor(readonly child: Error) {
5395
super()

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './errors'
2+
export * from './status'
23
export * from './types'
34

45
export { default, defaultOptions } from './plugin'

src/plugin.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
5656
switch (_options.strategy.type) {
5757
case 'URI': {
5858
const errorFunction = _options.strategy.onError
59+
const statusFunction = _options.strategy.onStatus
60+
const catchFunction = _options.strategy.onCatch
5961
const redirectDefault = _options.strategy.redirectDefault ?? true
6062

6163
for (const [version, handler] of versions) {
@@ -96,13 +98,16 @@ function versioning<TVersions extends Record<string, Elysia>>(
9698
const handler = _options.versions[defaultVersion]
9799
if (!handler) {
98100
errorFunction?.(new UriUnknownVersionError(defaultVersion))
101+
statusFunction?.('unknown_version')
99102
return
100103
}
101104

102105
try {
103106
return await handler.handle(request)
104107
} catch (error: unknown) {
105108
errorFunction?.(new UriUnexpectedError(error as Error))
109+
catchFunction?.(error as Error)
110+
statusFunction?.('unexpected')
106111
}
107112
}
108113
})
@@ -114,6 +119,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
114119
const queryName = _options.strategy.queryName ?? prefix
115120
const redirectDefault = _options.strategy.redirectDefault ?? true
116121
const errorFunction = _options.strategy.onError
122+
const statusFunction = _options.strategy.onStatus
123+
const catchFunction = _options.strategy.onCatch
117124

118125
plugin.onRequest(async ({ request }) => {
119126
const url = new URL(request.url)
@@ -124,27 +131,33 @@ function versioning<TVersions extends Record<string, Elysia>>(
124131
const handler = _options.versions[versionQueryParameter]
125132
if (!handler) {
126133
errorFunction?.(new QueryUnknownVersionError(versionQueryParameter))
134+
statusFunction?.('unknown_version')
127135
return
128136
}
129137

130138
try {
131139
return await handler.handle(request)
132140
} catch (error: unknown) {
133141
errorFunction?.(new QueryUnexpectedError(error as Error))
142+
catchFunction?.(error as Error)
143+
statusFunction?.('unexpected')
134144
}
135145
}
136146

137147
if (redirectDefault) {
138148
const defaultHandler = _options.versions[defaultVersion]
139149
if (!defaultHandler) {
140150
errorFunction?.(new QueryUnknownVersionError(defaultVersion))
151+
statusFunction?.('unknown_version')
141152
return
142153
}
143154

144155
try {
145156
return await defaultHandler.handle(request)
146157
} catch (error: unknown) {
147158
errorFunction?.(new QueryUnexpectedError(error as Error))
159+
catchFunction?.(error as Error)
160+
statusFunction?.('unexpected')
148161
}
149162
}
150163
})
@@ -154,6 +167,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
154167
case 'HEADER': {
155168
const headerName = _options.strategy.headerName ?? 'X-Version'
156169
const errorFunction = _options.strategy.onError
170+
const statusFunction = _options.strategy.onStatus
171+
const catchFunction = _options.strategy.onCatch
157172

158173
plugin.onRequest(async ({ request }) => {
159174
const header = request.headers.get(headerName)
@@ -162,6 +177,7 @@ function versioning<TVersions extends Record<string, Elysia>>(
162177
if (header) {
163178
if (!header.startsWith(prefix)) {
164179
errorFunction?.(new HeaderWrongPrefixError(prefix, header))
180+
statusFunction?.('wrong_prefix')
165181
return
166182
}
167183
version = header.replace(prefix, '')
@@ -170,13 +186,16 @@ function versioning<TVersions extends Record<string, Elysia>>(
170186
const handler = _options.versions[version]
171187
if (!handler) {
172188
errorFunction?.(new HeaderUnknownVersionError(version))
189+
statusFunction?.('unknown_version')
173190
return
174191
}
175192

176193
try {
177194
return await handler.handle(request)
178195
} catch (error: unknown) {
179196
errorFunction?.(new HeaderUnexpectedError(error as Error))
197+
catchFunction?.(error as Error)
198+
statusFunction?.('unexpected')
180199
}
181200
})
182201
break
@@ -185,6 +204,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
185204
case 'CUSTOM': {
186205
const extractFunction = _options.strategy.extract
187206
const errorFunction = _options.strategy.onError
207+
const statusFunction = _options.strategy.onStatus
208+
const catchFunction = _options.strategy.onCatch
188209

189210
plugin.onRequest(async ({ request }) => {
190211
try {
@@ -194,6 +215,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
194215
).handle(request)
195216
} catch (error: unknown) {
196217
errorFunction?.(new CustomUnexpectedError(error as Error))
218+
catchFunction?.(error as Error)
219+
statusFunction?.('unexpected')
197220
}
198221
})
199222
break

src/status.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type UriStatus = 'unknown_version' | 'unexpected';
2+
export type QueryStatus = 'unknown_version' | 'unexpected';
3+
export type HeaderStatus = 'wrong_prefix' | 'unknown_version' | 'unexpected';
4+
export type CustomStatus = 'unexpected';

src/types.ts

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import type Elysia from 'elysia'
22

3-
import type {
4-
CustomError,
5-
HeaderError,
6-
QueryError,
7-
UriError,
8-
} from './errors.ts'
3+
import type { CustomError, HeaderError, QueryError, UriError } from './errors.ts'
4+
import type { CustomStatus, HeaderStatus, QueryStatus, UriStatus } from './status.ts'
95

106
export type BaseVersioningOptions = VersioningOptions<Record<string, Elysia>>
117

@@ -42,24 +38,52 @@ export interface VersioningOptions<TVersions extends Record<string, Elysia>> {
4238
}
4339

4440
export type VersioningStrategy =
45-
| {
46-
type: 'URI'
47-
redirectDefault?: boolean
48-
onError?: (error: UriError) => void
49-
}
50-
| {
51-
type: 'QUERY'
52-
queryName?: string
53-
redirectDefault?: boolean
54-
onError?: (error: QueryError) => void
55-
}
56-
| {
57-
type: 'HEADER'
58-
headerName?: string
59-
onError?: (error: HeaderError) => void
60-
}
61-
| {
62-
type: 'CUSTOM'
63-
extract: (request: Request, options: BaseVersioningOptions) => Elysia
64-
onError?: (error: CustomError) => void
65-
}
41+
|
42+
{
43+
type: 'URI',
44+
redirectDefault?: boolean,
45+
/**
46+
* @deprecated Use the `onStatus` and `onCatch` handler instead.
47+
* This function will be removed in near future.
48+
*/
49+
onError?: (error: UriError) => void,
50+
onStatus?: (status: UriStatus) => void,
51+
onCatch?: (error: Error) => void,
52+
}
53+
|
54+
{
55+
type: 'QUERY',
56+
queryName?: string,
57+
redirectDefault?: boolean,
58+
/**
59+
* @deprecated Use the `onStatus` and `onCatch` handler instead.
60+
* This function will be removed in near future.
61+
*/
62+
onError?: (error: QueryError) => void,
63+
onStatus?: (status: QueryStatus) => void,
64+
onCatch?: (error: Error) => void,
65+
}
66+
|
67+
{
68+
type: 'HEADER',
69+
headerName?: string,
70+
/**
71+
* @deprecated Use the `onStatus` and `onCatch` handler instead.
72+
* This function will be removed in near future.
73+
*/
74+
onError?: (error: HeaderError) => void,
75+
onStatus?: (status: HeaderStatus) => void,
76+
onCatch?: (error: Error) => void,
77+
}
78+
|
79+
{
80+
type: 'CUSTOM',
81+
extract: (request: Request, options: BaseVersioningOptions) => Elysia,
82+
/**
83+
* @deprecated Use the `onStatus` and `onCatch` handler instead.
84+
* This function will be removed in near future.
85+
*/
86+
onError?: (error: CustomError) => void,
87+
onStatus?: (status: CustomStatus) => void,
88+
onCatch?: (error: Error) => void,
89+
}

0 commit comments

Comments
 (0)