Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "can not convert object to primitive" nonsence error message #697

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/umzug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ export class MigrationError extends errorCause.ErrorWithCause<unknown> {
}

private static errorString(cause: unknown) {
return cause instanceof Error
? `Original error: ${cause.message}`
: `Non-error value thrown. See info for full props: ${cause as string}`
if (cause instanceof Error) return `Original error: ${cause.message}`
const msg = 'Non-error value thrown. See info for full props'
if (typeof cause !== 'object' || (cause as {}).toString !== undefined) return `${msg}: ${String(cause)}`
//Null prototype object 'cause' would end up here
return msg
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/umzug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,44 @@ describe('alternate migration inputs', () => {
)
})

test('null-prototype throwables are wrapped helpfully', async () => {
// Errors derived from null prototype can not be printed with toString(), make sure we do not fail printing them
// This kind of error is thrown in MikroOrm migrations
const umzug = new Umzug({
migrations: [
{
name: 'm1',
async up() {},

async down() {
const reallyCrypticFailure: any = Object.create(null)
reallyCrypticFailure.customText = 'Some cryptic failure'
throw reallyCrypticFailure
},
},
{
name: 'm2',

async up() {
const reallyCrypticFailure: any = Object.create(null)
reallyCrypticFailure.customText = 'Some cryptic failure'
throw reallyCrypticFailure
},
async down() {},
},
],
logger: undefined,
})

await expect(umzug.up()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Migration m2 (up) failed: Non-error value thrown. See info for full props"`,
)

await expect(umzug.down()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Migration m1 (down) failed: Non-error value thrown. See info for full props"`,
)
})

test('typescript migration files', async () => {
require('ts-node/register')
const syncer = fsSyncer(path.join(__dirname, 'generated/umzug/typescript'), {
Expand Down