Skip to content

Commit 0fb6962

Browse files
authored
fix: add error handler on client (#429)
1 parent 46e7ff6 commit 0fb6962

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/database/migrate.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Client, ClientConfig } from 'pg'
22
import { loadMigrationFiles, MigrationError } from 'postgres-migrations'
33
import { getConfig } from '../config'
4-
import { logger } from '../monitoring'
4+
import { logger, logSchema } from '../monitoring'
55
import { BasicPgClient, Migration } from 'postgres-migrations/dist/types'
66
import { validateMigrationHashes } from 'postgres-migrations/dist/validation'
77
import { runMigration } from 'postgres-migrations/dist/run-migration'
@@ -44,15 +44,16 @@ export async function runMultitenantMigrations(): Promise<void> {
4444
/**
4545
* Runs migrations on a specific tenant by providing its database DSN
4646
* @param databaseUrl
47+
* @param tenantId
4748
*/
48-
export async function runMigrationsOnTenant(databaseUrl: string): Promise<void> {
49+
export async function runMigrationsOnTenant(databaseUrl: string, tenantId?: string): Promise<void> {
4950
let ssl: ClientConfig['ssl'] | undefined = undefined
5051

5152
if (databaseSSLRootCert) {
5253
ssl = { ca: databaseSSLRootCert }
5354
}
5455

55-
await connectAndMigrate(databaseUrl, './migrations/tenant', ssl)
56+
await connectAndMigrate(databaseUrl, './migrations/tenant', ssl, undefined, tenantId)
5657
}
5758

5859
/**
@@ -61,12 +62,14 @@ export async function runMigrationsOnTenant(databaseUrl: string): Promise<void>
6162
* @param migrationsDirectory
6263
* @param ssl
6364
* @param shouldCreateStorageSchema
65+
* @param tenantId
6466
*/
6567
async function connectAndMigrate(
6668
databaseUrl: string | undefined,
6769
migrationsDirectory: string,
6870
ssl?: ClientConfig['ssl'],
69-
shouldCreateStorageSchema?: boolean
71+
shouldCreateStorageSchema?: boolean,
72+
tenantId?: string
7073
) {
7174
const dbConfig: ClientConfig = {
7275
connectionString: databaseUrl,
@@ -76,6 +79,13 @@ async function connectAndMigrate(
7679
}
7780

7881
const client = new Client(dbConfig)
82+
client.on('error', (err) => {
83+
logSchema.error(logger, 'Error on database connection', {
84+
type: 'error',
85+
error: err,
86+
project: tenantId,
87+
})
88+
})
7989
try {
8090
await client.connect()
8191
await migrate({ client }, migrationsDirectory, shouldCreateStorageSchema)

src/database/tenant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function runMigrations(
5656
logOnError = false
5757
): Promise<void> {
5858
try {
59-
await runMigrationsOnTenant(databaseUrl)
59+
await runMigrationsOnTenant(databaseUrl, tenantId)
6060
console.log(`${tenantId} migrations ran successfully`)
6161
} catch (error: any) {
6262
if (logOnError) {

src/http/routes/tus/lifecycle.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ export async function onIncomingRequest(
3030
res: http.ServerResponse,
3131
id: string
3232
) {
33-
if (rawReq.method === 'OPTIONS') {
34-
return
35-
}
36-
3733
const req = rawReq as MultiPartRequest
3834
const uploadID = UploadId.fromString(id)
3935

@@ -43,14 +39,19 @@ export async function onIncomingRequest(
4339
})
4440
})
4541

42+
if (rawReq.method === 'OPTIONS') {
43+
return
44+
}
45+
4646
const isUpsert = req.headers['x-upsert'] === 'true'
4747

4848
const uploader = new Uploader(req.upload.storage.backend, req.upload.storage.db)
49+
4950
await uploader.canUpload({
5051
owner: req.upload.owner,
5152
bucketId: uploadID.bucket,
5253
objectName: uploadID.objectName,
53-
isUpsert,
54+
isUpsert: isUpsert,
5455
})
5556
}
5657

src/monitoring/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface EventLog {
5757
interface ErrorLog {
5858
type: string
5959
error?: Error | unknown
60+
project?: string
6061
}
6162

6263
export const logSchema = {

0 commit comments

Comments
 (0)