Skip to content

Commit

Permalink
🎉 feat: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Sep 20, 2023
2 parents ed8dc29 + 1fa8a4c commit 79f8698
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: 'npm'
directory: './'
schedule:
interval: 'daily'

- package-ecosystem: 'github-actions'
directory: './'
schedule:
interval: 'daily'
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Code CI

on:
push:
pull_request:

jobs:
build:
name: Build and test code
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Install packages
run: bun install

- name: Build code
run: bun run build

- name: Test
run: bun run test
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -39,7 +39,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: 'Setup Bun'
uses: oven-sh/setup-bun@v1
Expand Down
14 changes: 7 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ export type ElysiaErrors =
| ValidationError

export class InternalServerError extends Error {
code = 'NOT_FOUND'
code = 'INTERNAL_SERVER_ERROR'
status = 500

constructor() {
super('INTERNAL_SERVER_ERROR')
constructor(message?: string) {
super(message ?? 'INTERNAL_SERVER_ERROR')
}
}

export class NotFoundError extends Error {
code = 'NOT_FOUND'
status = 404

constructor() {
super('NOT_FOUND')
constructor(message?: string) {
super(message ?? 'NOT_FOUND')
}
}

export class ParseError extends Error {
code = 'PARSE'
status = 400

constructor() {
super('PARSE')
constructor(message?: string) {
super(message ?? 'PARSE')
}
}

Expand Down
19 changes: 18 additions & 1 deletion test/lifecycle/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Elysia, t } from '../../src'
import { Elysia, InternalServerError, t } from '../../src'
import { describe, expect, it } from 'bun:test'
import { post, req } from '../utils'

Expand Down Expand Up @@ -71,4 +71,21 @@ describe('error', () => {
expect(data.length).toBe(4)
expect(res.status).toBe(400)
})

it('custom 500', async () => {
const app = new Elysia()
.onError(({ code }) => {
if (code === 'INTERNAL_SERVER_ERROR') {
return 'UwU'
}
})
.get('/', () => {
throw new InternalServerError()
})

const response = await app.handle(req('/'))

expect(await response.text()).toBe('UwU')
expect(response.status).toBe(500)
})
})

0 comments on commit 79f8698

Please sign in to comment.