Skip to content

Commit

Permalink
fix: add error handler on client (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos authored Jan 26, 2024
1 parent 46e7ff6 commit 0fb6962
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, ClientConfig } from 'pg'
import { loadMigrationFiles, MigrationError } from 'postgres-migrations'
import { getConfig } from '../config'
import { logger } from '../monitoring'
import { logger, logSchema } from '../monitoring'
import { BasicPgClient, Migration } from 'postgres-migrations/dist/types'
import { validateMigrationHashes } from 'postgres-migrations/dist/validation'
import { runMigration } from 'postgres-migrations/dist/run-migration'
Expand Down Expand Up @@ -44,15 +44,16 @@ export async function runMultitenantMigrations(): Promise<void> {
/**
* Runs migrations on a specific tenant by providing its database DSN
* @param databaseUrl
* @param tenantId
*/
export async function runMigrationsOnTenant(databaseUrl: string): Promise<void> {
export async function runMigrationsOnTenant(databaseUrl: string, tenantId?: string): Promise<void> {
let ssl: ClientConfig['ssl'] | undefined = undefined

if (databaseSSLRootCert) {
ssl = { ca: databaseSSLRootCert }
}

await connectAndMigrate(databaseUrl, './migrations/tenant', ssl)
await connectAndMigrate(databaseUrl, './migrations/tenant', ssl, undefined, tenantId)
}

/**
Expand All @@ -61,12 +62,14 @@ export async function runMigrationsOnTenant(databaseUrl: string): Promise<void>
* @param migrationsDirectory
* @param ssl
* @param shouldCreateStorageSchema
* @param tenantId
*/
async function connectAndMigrate(
databaseUrl: string | undefined,
migrationsDirectory: string,
ssl?: ClientConfig['ssl'],
shouldCreateStorageSchema?: boolean
shouldCreateStorageSchema?: boolean,
tenantId?: string
) {
const dbConfig: ClientConfig = {
connectionString: databaseUrl,
Expand All @@ -76,6 +79,13 @@ async function connectAndMigrate(
}

const client = new Client(dbConfig)
client.on('error', (err) => {
logSchema.error(logger, 'Error on database connection', {
type: 'error',
error: err,
project: tenantId,
})
})
try {
await client.connect()
await migrate({ client }, migrationsDirectory, shouldCreateStorageSchema)
Expand Down
2 changes: 1 addition & 1 deletion src/database/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function runMigrations(
logOnError = false
): Promise<void> {
try {
await runMigrationsOnTenant(databaseUrl)
await runMigrationsOnTenant(databaseUrl, tenantId)
console.log(`${tenantId} migrations ran successfully`)
} catch (error: any) {

Check warning on line 61 in src/database/tenant.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-20.04 / Node 20

Unexpected any. Specify a different type
if (logOnError) {
Expand Down
11 changes: 6 additions & 5 deletions src/http/routes/tus/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export async function onIncomingRequest(
res: http.ServerResponse,
id: string
) {
if (rawReq.method === 'OPTIONS') {
return
}

const req = rawReq as MultiPartRequest
const uploadID = UploadId.fromString(id)

Expand All @@ -43,14 +39,19 @@ export async function onIncomingRequest(
})
})

if (rawReq.method === 'OPTIONS') {
return
}

const isUpsert = req.headers['x-upsert'] === 'true'

const uploader = new Uploader(req.upload.storage.backend, req.upload.storage.db)

await uploader.canUpload({
owner: req.upload.owner,
bucketId: uploadID.bucket,
objectName: uploadID.objectName,
isUpsert,
isUpsert: isUpsert,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/monitoring/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface EventLog {
interface ErrorLog {
type: string
error?: Error | unknown
project?: string
}

export const logSchema = {
Expand Down

0 comments on commit 0fb6962

Please sign in to comment.