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

Enhancement: Print stack trace on errors and improve error messages #169

Merged
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
28 changes: 14 additions & 14 deletions ConvertOneNote2MarkDown-v2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1651,11 +1651,11 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {
}

It "Halts converting if creation of any directory fails" {
Mock New-Item -ParameterFilter { $ItemType -eq 'Directory' -and $Force } { throw }
Mock New-Item -ParameterFilter { $ItemType -eq 'Directory' -and $Force } { throw "i failed" }

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to convert page'
$err | Should -Not -Be $null
}

It "Removes existing docx by default" {
Expand All @@ -1668,13 +1668,13 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {

It "Halts converting if removal of existing docx fails" {
Mock Test-Path -ParameterFilter { $LiteralPath } { $true }
Mock Remove-Item -ParameterFilter { $LiteralPath -and $Force } { throw }
Mock Remove-Item -ParameterFilter { $LiteralPath -and $Force } { throw "i failed" }

$err = Convert-OneNotePage @params 6>$null 2>&1

$err | Should -Not -Be $null
Assert-MockCalled -CommandName Test-Path -ParameterFilter { $LiteralPath } -Times 1 -Scope It
Assert-MockCalled -CommandName Remove-Item -ParameterFilter { $LiteralPath -and $Force } -Times 1 -Scope It
$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to convert page'
}

It "Publishes OneNote page to Word" {
Expand All @@ -1684,11 +1684,11 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {
}

It "Halts converting if publish OneNote page to Word fails" {
Mock Publish-OneNotePage -ParameterFilter { $PublishFormat -eq 'pfWord' } { throw }
Mock Publish-OneNotePage -ParameterFilter { $PublishFormat -eq 'pfWord' } { throw "i failed" }

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to convert page'
$err | Should -Not -Be $null
}

It "Publishes OneNote page to pdf" {
Expand All @@ -1707,15 +1707,15 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {
Assert-MockCalled -CommandName Start-Process -Times 1 -Scope It
}

It "Halts converting if pandoc conversion from docx to markdown fails" {
Mock Start-Process { throw }
It "Halts converting if executing pandoc fails" {
Mock Start-Process { throw "i failed" }

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to convert page'
$err | Should -Not -Be $null
}

It "Logs pandoc errors" {
It "Halts converting on pandoc errors" {
Mock Start-Process {
[PSCustomObject]@{
ExitCode = 1
Expand All @@ -1727,7 +1727,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'i am some error from pandoc'
$err | Should -Not -Be $null
}

It "Saves page attachment(s)" {
Expand All @@ -1741,7 +1741,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Error while saving attachment'
$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to save attachment'
}

It "Rename page image(s) to unique names" {
Expand All @@ -1756,7 +1756,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Error while renaming image'
$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to rename image'
}

It "Markdown Mutation: Rename page image references in markdown to unique names" {
Expand All @@ -1771,7 +1771,7 @@ Describe 'Convert-OneNotePage' -Tag 'Unit' {

$err = Convert-OneNotePage @params 6>$null 2>&1

$err.Exception.Message | Select-Object -First 1 | Should -match 'Error while renaming image file name references to'
$err.Exception.Message | Select-Object -First 1 | Should -match 'Failed to rename image references to'
}

It "Markdown mutation: Performs mutations on markdown content" {
Expand Down
45 changes: 27 additions & 18 deletions ConvertOneNote2MarkDown-v2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,17 @@ Function New-OneNoteConnection {
if ($OneNote = New-Object -ComObject OneNote.Application) {
$OneNote
}else {
throw "Failed to make connection to OneNote."
Write-Error "Failed to make connection to OneNote." -ErrorAction Continue
throw
}
}else {
# Works between powershell 5.x (possibly lower) and 7.0, but not >= 7.1. 7.1 and above doesn't seem to support loading Win32 GAC Assemblies.
if (Add-Type -Path $env:windir\assembly\GAC_MSIL\Microsoft.Office.Interop.OneNote\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.OneNote.dll -PassThru) {
$OneNote = [Microsoft.Office.Interop.OneNote.ApplicationClass]::new()
$OneNote
}else {
throw "Failed to make connection to OneNote."
Write-Error "Failed to make connection to OneNote." -ErrorAction Continue
throw
}
}
}
Expand Down Expand Up @@ -1104,7 +1106,8 @@ Function Convert-OneNotePage {
$item = New-Item -Path $d -ItemType Directory -Force -ErrorAction Stop
}
}catch {
throw "Failed to create directory $d. Exception: $( $_.Exception.Message )"
Write-Error "Failed to create directory $d" -ErrorAction Continue
throw
}
}

Expand All @@ -1118,7 +1121,8 @@ Function Convert-OneNotePage {
}
}
}catch {
throw "Error removing intermediary docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to remove existing docx file $( $pageCfg['docxExportFilePath'] )" -ErrorAction Continue
throw
}
}

Expand All @@ -1130,7 +1134,8 @@ Function Convert-OneNotePage {
Publish-OneNotePage -OneNoteConnection $OneNoteConnection -PageId $pageCfg['object'].ID -Destination $pageCfg['docxExportFilePath'] -PublishFormat 'pfWord'
}
}catch {
throw "Error while publishing page to docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to publish page to docx file $( $pageCfg['docxExportFilePath'] )" -ErrorAction Continue
throw
}
}else {
"Existing docx file: $( $pageCfg['docxExportFilePath'] )" | Write-Verbose
Expand All @@ -1147,7 +1152,8 @@ Function Convert-OneNotePage {
}
"pdf file ready: $( $pageCfg['pdfExportFilePath'] )" | Write-Host -ForegroundColor Green
}catch {
throw "Error while publishing page to pdf file $( $pageCfg['pdfExportFilePath'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to publish page to pdf file $( $pageCfg['pdfExportFilePath'] )" -ErrorAction Continue
throw
}
}else {
"Existing pdf file: $( $pageCfg['pdfExportFilePath'] )" | Write-Host -ForegroundColor Green
Expand Down Expand Up @@ -1190,11 +1196,12 @@ Function Convert-OneNotePage {
$process = Start-Process -ErrorAction Stop -RedirectStandardError $stderrFile -PassThru -NoNewWindow -Wait -FilePath pandoc.exe -ArgumentList $argumentList # extracts into ./media of the supplied folder
if ($process.ExitCode -ne 0) {
$stderr = Get-Content $stderrFile -Raw
throw "pandoc failed to convert: $stderr"
throw "pandoc error: $stderr"
}
}
}catch {
throw "Error while converting docx file $( $pageCfg['docxExportFilePath'] ) to markdown file $( $pageCfg['filePathNormal'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to convert docx file $( $pageCfg['docxExportFilePath'] ) to markdown file $( $pageCfg['filePathNormal'] )"
throw
}finally {
if (Test-Path $stderrFile) {
Remove-Item $stderrFile -Force
Expand All @@ -1211,7 +1218,7 @@ Function Convert-OneNotePage {
}
}
}catch {
Write-Error "Error removing intermediary docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to remove existing docx file $( $pageCfg['docxExportFilePath'] ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
}

Expand All @@ -1223,7 +1230,7 @@ Function Convert-OneNotePage {
Copy-Item -Path $attachmentCfg['source'] -Destination $attachmentCfg['destination'] -Force -ErrorAction Stop
}
}catch {
Write-Error "Error while saving attachment from $( $attachmentCfg['source'] ) to $( $attachmentCfg['destination'] ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to save attachment from $( $attachmentCfg['source'] ) to $( $attachmentCfg['destination'] ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
}

Expand All @@ -1244,18 +1251,18 @@ Function Convert-OneNotePage {
$item = Move-Item -Path "$( $image.FullName )" -Destination $newimagePath -Force -ErrorAction Stop -PassThru
}
}catch {
Write-Error "Error while renaming image $( $image.FullName ) to $( $item.FullName ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to rename image $( $image.FullName ) to $( $item.FullName ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
# Change MD file Image filename References
# Mutate markdown content with new image references
try {
"Mutation of markdown: Rename image references to unique name. Find '$( $image.Name )', Replacement: '$( $newimageName )'" | Write-Verbose
"Mutation of markdown: Rename image references. Find: '$( $image.Name )', Replacement: '$( $newimageName )'" | Write-Verbose
if (!$config['dryRun']['value']) {
$content = Get-Content -LiteralPath $pageCfg['filePath'] -Raw -ErrorAction Stop # Use -LiteralPath so that characters like '(', ')', '[', ']', '`', "'", '"' are supported. Or else we will get an error "Cannot find path 'xxx' because it does not exist"
$content = $content.Replace("$($image.Name)", "$($newimageName)")
Set-ContentNoBom -LiteralPath $pageCfg['filePath'] -Value $content -ErrorAction Stop # Use -LiteralPath so that characters like '(', ')', '[', ']', '`', "'", '"' are supported. Or else we will get an error "Cannot find path 'xxx' because it does not exist"
}
}catch {
Write-Error "Error while renaming image file name references to $( $newimageName ). Exception: $( $_.Exception.Message )"
Write-Error "Failed to rename image references to $( $newimageName ). Exception: $( $_.Exception.Message )" -ErrorAction Continue
}
}
}
Expand Down Expand Up @@ -1285,21 +1292,23 @@ Function Convert-OneNotePage {
$content = $content -replace $r['searchRegex'], $r['replacement']
}
}catch {
Write-Error "Failed to mutating markdown content with mutation '$( $m['description'] )'. Exception: $( $_.Exception.Message )"
Write-Error "Failed to mutate markdown content with mutation '$( $m['description'] )'. Exception: $( $_.Exception.Message )"
}
}
}
if (!$config['dryRun']['value']) {
Set-ContentNoBom -LiteralPath $pageCfg['filePath'] -Value $content -ErrorAction Stop # Use -LiteralPath so that characters like '(', ')', '[', ']', '`', "'", '"' are supported. Or else we will get an error "Cannot find path 'xxx' because it does not exist"
}
}catch {
Write-Error "Error while mutating markdown content: $( $_.Exception.Message )"
Write-Error "Failed to mutate markdown content: $( $_.Exception.Message )"
}

"Markdown file ready: $( $pageCfg['filePathNormal'] )" | Write-Host -ForegroundColor Green
}catch {
Write-Host "Failed to convert page: $( $pageCfg['pathFromRoot'] ). Exception: $( $_.Exception.Message )" -ForegroundColor Red
Write-Error "Failed to convert page: $( $pageCfg['pathFromRoot'] ). Exception: $( $_.Exception.Message )"
# Don't throw terminating errors from this function
Write-Error "Failed to convert page: $( $pageCfg['pathFromRoot'] )"
Write-Error -Message $_.Exception.Message
Write-Error -Message $_.ScriptStackTrace
}
}
}
Expand Down