-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle error with customAssert
- Loading branch information
Showing
10 changed files
with
42 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import { CustomError } from 'service/customAssert'; | ||
import { prismaClient } from 'service/prismaClient'; | ||
import { s3 } from 'service/s3Client'; | ||
import { defineController } from './$relay'; | ||
|
||
const throwCustomError = (label: string) => (e: Error) => { | ||
/* v8 ignore next 2 */ | ||
throw new CustomError(`${label} ${e.message}`); | ||
}; | ||
|
||
export default defineController(() => ({ | ||
get: async () => ({ | ||
status: 200, | ||
body: { | ||
server: 'ok', | ||
db: await prismaClient.$queryRaw`SELECT CURRENT_TIMESTAMP;`.then(() => 'ok' as const), | ||
storage: await s3.health().then(() => 'ok' as const), | ||
db: await prismaClient.$queryRaw`SELECT CURRENT_TIMESTAMP;` | ||
.then(() => 'ok' as const) | ||
.catch(throwCustomError('DB')), | ||
s3: await s3 | ||
.health() | ||
.then(() => 'ok' as const) | ||
.catch(throwCustomError('S3')), | ||
}, | ||
}), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ module.exports = { | |
bundle: true, | ||
plugins: [nodeExternalsPlugin()], | ||
logLevel: 'info', | ||
sourcemap: 'linked', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import assert from 'assert'; | ||
|
||
export class CustomError extends Error {} | ||
|
||
export function customAssert(val: unknown, msg: string): asserts val { | ||
assert(val, new CustomError(msg)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters