Skip to content

Commit 0e7d02f

Browse files
authored
Allow single quotes in schema error description (#26)
* Allow schema error with description containing single quote character * Add few schema errors to example RIDL & regenerate
1 parent 7d92406 commit 0e7d02f

File tree

5 files changed

+139
-39
lines changed

5 files changed

+139
-39
lines changed

_examples/node-ts/server/server.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable */
2-
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
2+
// node-ts v1.0.0 f5566a7aae2c4d2f76cc64d8e55f9ac841447e43
33
// --
4-
// Code generated by webrpc-gen@v0.21.4 with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by webrpc-gen@v0.22.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -server -out=./server/server.gen.ts
77

88
export const WebrpcHeader = "Webrpc"
99

10-
export const WebrpcHeaderValue = "webrpc@v0.21.4;gen-typescript@unknown;[email protected]"
10+
export const WebrpcHeaderValue = "webrpc@v0.22.0;gen-typescript@unknown;[email protected]"
1111

1212
// WebRPC description and code-gen version
1313
export const WebRPCVersion = "v1"
@@ -16,7 +16,7 @@ export const WebRPCVersion = "v1"
1616
export const WebRPCSchemaVersion = "v1.0.0"
1717

1818
// Schema hash generated from your RIDL schema
19-
export const WebRPCSchemaHash = "6713366104e62b8479d628a193e2a7ca03f37edc"
19+
export const WebRPCSchemaHash = "f5566a7aae2c4d2f76cc64d8e55f9ac841447e43"
2020

2121
type WebrpcGenVersions = {
2222
webrpcGenVersion: string;

_examples/node-ts/service.ridl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ webrpc = v1
33
name = node-ts
44
version = v1.0.0
55

6-
76
enum Kind: uint32
87
- USER
98
- ADMIN
109

11-
1210
struct User
1311
- ID: uint64
1412
+ json = id
@@ -25,11 +23,17 @@ struct User
2523

2624
- createdAt?: timestamp
2725

28-
2926
struct Page
3027
- num: uint32
3128

32-
3329
service ExampleService
3430
- Ping() => ()
3531
- GetUser(userID: uint64) => (code: uint32, user: User)
32+
33+
error 1000 Unauthorized "Unauthorized access" HTTP 401
34+
error 1001 PermissionDenied "Permission denied" HTTP 403
35+
error 1002 SessionExpired "Session expired" HTTP 403
36+
error 1003 Geoblocked "Geoblocked region" HTTP 451
37+
error 1004 RateLimited "Rate-limited. Please slow down." HTTP 429
38+
error 1005 CorsDisallowed "CORS disallowed. JWT can't be used from a web app." HTTP 403
39+

_examples/node-ts/webapp/client.gen.ts

Lines changed: 111 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable */
2-
// node-ts v1.0.0 6713366104e62b8479d628a193e2a7ca03f37edc
2+
// node-ts v1.0.0 f5566a7aae2c4d2f76cc64d8e55f9ac841447e43
33
// --
4-
// Code generated by webrpc-gen@v0.21.4 with ../../../gen-typescript generator. DO NOT EDIT.
4+
// Code generated by webrpc-gen@v0.22.0 with ../../../gen-typescript generator. DO NOT EDIT.
55
//
66
// webrpc-gen -schema=service.ridl -target=../../../gen-typescript -client -out=./webapp/client.gen.ts
77

88
export const WebrpcHeader = "Webrpc"
99

10-
export const WebrpcHeaderValue = "webrpc@v0.21.4;gen-typescript@unknown;[email protected]"
10+
export const WebrpcHeaderValue = "webrpc@v0.22.0;gen-typescript@unknown;[email protected]"
1111

1212
// WebRPC description and code-gen version
1313
export const WebRPCVersion = "v1"
@@ -16,7 +16,7 @@ export const WebRPCVersion = "v1"
1616
export const WebRPCSchemaVersion = "v1.0.0"
1717

1818
// Schema hash generated from your RIDL schema
19-
export const WebRPCSchemaHash = "6713366104e62b8479d628a193e2a7ca03f37edc"
19+
export const WebRPCSchemaHash = "f5566a7aae2c4d2f76cc64d8e55f9ac841447e43"
2020

2121
type WebrpcGenVersions = {
2222
webrpcGenVersion: string;
@@ -227,7 +227,7 @@ export class WebrpcEndpointError extends WebrpcError {
227227
constructor(
228228
name: string = 'WebrpcEndpoint',
229229
code: number = 0,
230-
message: string = 'endpoint error',
230+
message: string = `endpoint error`,
231231
status: number = 0,
232232
cause?: string
233233
) {
@@ -240,7 +240,7 @@ export class WebrpcRequestFailedError extends WebrpcError {
240240
constructor(
241241
name: string = 'WebrpcRequestFailed',
242242
code: number = -1,
243-
message: string = 'request failed',
243+
message: string = `request failed`,
244244
status: number = 0,
245245
cause?: string
246246
) {
@@ -253,7 +253,7 @@ export class WebrpcBadRouteError extends WebrpcError {
253253
constructor(
254254
name: string = 'WebrpcBadRoute',
255255
code: number = -2,
256-
message: string = 'bad route',
256+
message: string = `bad route`,
257257
status: number = 0,
258258
cause?: string
259259
) {
@@ -266,7 +266,7 @@ export class WebrpcBadMethodError extends WebrpcError {
266266
constructor(
267267
name: string = 'WebrpcBadMethod',
268268
code: number = -3,
269-
message: string = 'bad method',
269+
message: string = `bad method`,
270270
status: number = 0,
271271
cause?: string
272272
) {
@@ -279,7 +279,7 @@ export class WebrpcBadRequestError extends WebrpcError {
279279
constructor(
280280
name: string = 'WebrpcBadRequest',
281281
code: number = -4,
282-
message: string = 'bad request',
282+
message: string = `bad request`,
283283
status: number = 0,
284284
cause?: string
285285
) {
@@ -292,7 +292,7 @@ export class WebrpcBadResponseError extends WebrpcError {
292292
constructor(
293293
name: string = 'WebrpcBadResponse',
294294
code: number = -5,
295-
message: string = 'bad response',
295+
message: string = `bad response`,
296296
status: number = 0,
297297
cause?: string
298298
) {
@@ -305,7 +305,7 @@ export class WebrpcServerPanicError extends WebrpcError {
305305
constructor(
306306
name: string = 'WebrpcServerPanic',
307307
code: number = -6,
308-
message: string = 'server panic',
308+
message: string = `server panic`,
309309
status: number = 0,
310310
cause?: string
311311
) {
@@ -318,7 +318,7 @@ export class WebrpcInternalErrorError extends WebrpcError {
318318
constructor(
319319
name: string = 'WebrpcInternalError',
320320
code: number = -7,
321-
message: string = 'internal error',
321+
message: string = `internal error`,
322322
status: number = 0,
323323
cause?: string
324324
) {
@@ -331,7 +331,7 @@ export class WebrpcClientDisconnectedError extends WebrpcError {
331331
constructor(
332332
name: string = 'WebrpcClientDisconnected',
333333
code: number = -8,
334-
message: string = 'client disconnected',
334+
message: string = `client disconnected`,
335335
status: number = 0,
336336
cause?: string
337337
) {
@@ -344,7 +344,7 @@ export class WebrpcStreamLostError extends WebrpcError {
344344
constructor(
345345
name: string = 'WebrpcStreamLost',
346346
code: number = -9,
347-
message: string = 'stream lost',
347+
message: string = `stream lost`,
348348
status: number = 0,
349349
cause?: string
350350
) {
@@ -357,7 +357,7 @@ export class WebrpcStreamFinishedError extends WebrpcError {
357357
constructor(
358358
name: string = 'WebrpcStreamFinished',
359359
code: number = -10,
360-
message: string = 'stream finished',
360+
message: string = `stream finished`,
361361
status: number = 0,
362362
cause?: string
363363
) {
@@ -369,6 +369,84 @@ export class WebrpcStreamFinishedError extends WebrpcError {
369369

370370
// Schema errors
371371

372+
export class UnauthorizedError extends WebrpcError {
373+
constructor(
374+
name: string = 'Unauthorized',
375+
code: number = 1000,
376+
message: string = `Unauthorized access`,
377+
status: number = 0,
378+
cause?: string
379+
) {
380+
super(name, code, message, status, cause)
381+
Object.setPrototypeOf(this, UnauthorizedError.prototype)
382+
}
383+
}
384+
385+
export class PermissionDeniedError extends WebrpcError {
386+
constructor(
387+
name: string = 'PermissionDenied',
388+
code: number = 1001,
389+
message: string = `Permission denied`,
390+
status: number = 0,
391+
cause?: string
392+
) {
393+
super(name, code, message, status, cause)
394+
Object.setPrototypeOf(this, PermissionDeniedError.prototype)
395+
}
396+
}
397+
398+
export class SessionExpiredError extends WebrpcError {
399+
constructor(
400+
name: string = 'SessionExpired',
401+
code: number = 1002,
402+
message: string = `Session expired`,
403+
status: number = 0,
404+
cause?: string
405+
) {
406+
super(name, code, message, status, cause)
407+
Object.setPrototypeOf(this, SessionExpiredError.prototype)
408+
}
409+
}
410+
411+
export class GeoblockedError extends WebrpcError {
412+
constructor(
413+
name: string = 'Geoblocked',
414+
code: number = 1003,
415+
message: string = `Geoblocked region`,
416+
status: number = 0,
417+
cause?: string
418+
) {
419+
super(name, code, message, status, cause)
420+
Object.setPrototypeOf(this, GeoblockedError.prototype)
421+
}
422+
}
423+
424+
export class RateLimitedError extends WebrpcError {
425+
constructor(
426+
name: string = 'RateLimited',
427+
code: number = 1004,
428+
message: string = `Rate-limited. Please slow down.`,
429+
status: number = 0,
430+
cause?: string
431+
) {
432+
super(name, code, message, status, cause)
433+
Object.setPrototypeOf(this, RateLimitedError.prototype)
434+
}
435+
}
436+
437+
export class CorsDisallowedError extends WebrpcError {
438+
constructor(
439+
name: string = 'CorsDisallowed',
440+
code: number = 1005,
441+
message: string = `CORS disallowed. JWT can't be used from a web app.`,
442+
status: number = 0,
443+
cause?: string
444+
) {
445+
super(name, code, message, status, cause)
446+
Object.setPrototypeOf(this, CorsDisallowedError.prototype)
447+
}
448+
}
449+
372450

373451
export enum errors {
374452
WebrpcEndpoint = 'WebrpcEndpoint',
@@ -382,6 +460,12 @@ export enum errors {
382460
WebrpcClientDisconnected = 'WebrpcClientDisconnected',
383461
WebrpcStreamLost = 'WebrpcStreamLost',
384462
WebrpcStreamFinished = 'WebrpcStreamFinished',
463+
Unauthorized = 'Unauthorized',
464+
PermissionDenied = 'PermissionDenied',
465+
SessionExpired = 'SessionExpired',
466+
Geoblocked = 'Geoblocked',
467+
RateLimited = 'RateLimited',
468+
CorsDisallowed = 'CorsDisallowed',
385469
}
386470

387471
export enum WebrpcErrorCodes {
@@ -396,6 +480,12 @@ export enum WebrpcErrorCodes {
396480
WebrpcClientDisconnected = -8,
397481
WebrpcStreamLost = -9,
398482
WebrpcStreamFinished = -10,
483+
Unauthorized = 1000,
484+
PermissionDenied = 1001,
485+
SessionExpired = 1002,
486+
Geoblocked = 1003,
487+
RateLimited = 1004,
488+
CorsDisallowed = 1005,
399489
}
400490

401491
export const webrpcErrorByCode: { [code: number]: any } = {
@@ -410,6 +500,12 @@ export const webrpcErrorByCode: { [code: number]: any } = {
410500
[-8]: WebrpcClientDisconnectedError,
411501
[-9]: WebrpcStreamLostError,
412502
[-10]: WebrpcStreamFinishedError,
503+
[1000]: UnauthorizedError,
504+
[1001]: PermissionDeniedError,
505+
[1002]: SessionExpiredError,
506+
[1003]: GeoblockedError,
507+
[1004]: RateLimitedError,
508+
[1005]: CorsDisallowedError,
413509
}
414510

415511
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>

0 commit comments

Comments
 (0)