Skip to content

Commit e6c23ce

Browse files
committed
Improve error handling
1 parent a2d5cdf commit e6c23ce

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

mdPrintImages.bas

+7-7
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Public Function PrintImages( _
143143
'--- setup output file
144144
hDC = CreateDC("", sPrinterName, 0, baDevMode(0))
145145
If hDC = 0 Then
146-
sError = GetSystemMessage(Err.LastDllError)
146+
sError = GetSystemMessage(Err.LastDllError) & " [CreateDC]"
147147
GoTo QH
148148
End If
149149
uInfo.cbSize = LenB(uInfo)
@@ -161,7 +161,7 @@ Public Function PrintImages( _
161161
lHeight = GetDeviceCaps(hDC, VERTRES) - lTop - C_Dbl(At(vMargins, 3)) * lDpiY
162162
'--- output images
163163
If StartDoc(hDC, uInfo) <= 0 Then
164-
sError = GetSystemMessage(Err.LastDllError)
164+
sError = GetSystemMessage(Err.LastDllError) & " [StartDoc]"
165165
GoTo QH
166166
End If
167167
uHeader.biSize = LenB(uHeader)
@@ -201,7 +201,7 @@ QH:
201201
End If
202202
Exit Function
203203
EH:
204-
sError = "[&H" & Hex(Err.Number) & "] Critical: " & Err.Description & " [PrintImages]"
204+
sError = "Critical error: " & Err.Description & " (0x" & Hex(Err.Number) & ") [PrintImages]"
205205
Resume QH
206206
End Function
207207

@@ -252,7 +252,7 @@ QH:
252252
End If
253253
Exit Function
254254
EH:
255-
sError = "[&H" & Hex(Err.Number) & "] Critical: " & Err.Description & " [SetupDevMode]"
255+
sError = "Critical error: " & Err.Description & " (0x" & Hex(Err.Number) & ") [SetupDevMode]"
256256
Resume QH
257257
End Function
258258

@@ -264,7 +264,7 @@ Private Function GetImageDimensions(sFile As String, lWidth As Long, lHeight As
264264
On Error GoTo EH
265265
If GdipLoadImageFromFile(StrPtr(sFile), hBitmap) <> 0 Then
266266
If Err.LastDllError = 0 Then
267-
sError = "Invalid image: " & Mid$(sFile, InStrRev(sFile, "\") + 1) & " [GdipLoadImageFromFile]"
267+
sError = "File '" & Mid$(sFile, InStrRev(sFile, "\") + 1) & "' is invalid image [GdipLoadImageFromFile]"
268268
Else
269269
sError = GetSystemMessage(Err.LastDllError) & " [GdipLoadImageFromFile]"
270270
End If
@@ -284,7 +284,7 @@ QH:
284284
End If
285285
Exit Function
286286
EH:
287-
sError = "[&H" & Hex(Err.Number) & "] Critical: " & Err.Description & " [GetImageDimensions]"
287+
sError = "Critical error: " & Err.Description & " (0x" & Hex(Err.Number) & ") [GetImageDimensions]"
288288
Resume QH
289289
End Function
290290

@@ -336,7 +336,7 @@ Private Function GetSystemMessage(ByVal lLastDllError As Long) As String
336336
lSize = lSize - 2
337337
End If
338338
End If
339-
GetSystemMessage = "[" & lLastDllError & "] " & Left$(GetSystemMessage, lSize)
339+
GetSystemMessage = Left$(GetSystemMessage, lSize) & " (" & lLastDllError & ")"
340340
End Function
341341

342342
Private Function At(vArray As Variant, ByVal lIdx As Long) As Variant

mdStartup.bas

+17-7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Private Function Process(vArgs As Variant) As Long
7676
Dim uPapers() As UcsPaperInfoType
7777
Dim sText As String
7878

79+
On Error GoTo EH
7980
Set m_oOpt = GetOpt(vArgs, "printer:orientation:paper:margins:o")
8081
If Not m_oOpt.Item("-nologo") And Not m_oOpt.Item("-q") Then
8182
ConsoleError App.ProductName & " " & App.Major & "." & App.Minor & " (c) 2018 by [email protected]" & vbCrLf
@@ -98,7 +99,7 @@ Private Function Process(vArgs As Variant) As Long
9899
" -q in quiet operation outputs only errors" & vbCrLf & _
99100
" -nologo suppress startup banner" & vbCrLf
100101
If m_oOpt.Item("#arg") < 0 Then
101-
Process = 100
102+
Process = 1
102103
End If
103104
GoTo QH
104105
End If
@@ -120,6 +121,11 @@ Private Function Process(vArgs As Variant) As Long
120121
End If
121122
End If
122123
Next
124+
If cFiles.Count = 0 Then
125+
ConsoleError "No input files found" & vbCrLf
126+
Process = 1
127+
GoTo QH
128+
End If
123129
ReDim vInputFiles(0 To cFiles.Count - 1) As String
124130
For lIdx = 1 To cFiles.Count
125131
vInputFiles(lIdx - 1) = cFiles.Item(lIdx)
@@ -147,10 +153,10 @@ Private Function Process(vArgs As Variant) As Long
147153
Next
148154
End If
149155
If lPaperSize = 0 Then
156+
If LenB(sText) <> 0 Then
157+
sText = ". Not from " & Mid$(sText, 3)
158+
End If
150159
If Not m_oOpt.Item("-q") Then
151-
If LenB(sText) <> 0 Then
152-
sText = ". Not from " & Mid$(sText, 3)
153-
End If
154160
ConsoleError "Warning: '%1' paper ignored" & sText & vbCrLf, m_oOpt.Item("-paper")
155161
End If
156162
End If
@@ -170,7 +176,7 @@ Private Function Process(vArgs As Variant) As Long
170176
lOrientation:=lOrientation, _
171177
vMargins:=vMargins, _
172178
sError:=sError) Then
173-
ConsoleError sError & vbCrLf & vbCrLf
179+
ConsoleError sError & vbCrLf
174180
Process = 2
175181
GoTo QH
176182
End If
@@ -182,10 +188,14 @@ Private Function Process(vArgs As Variant) As Long
182188
Next
183189
If FileExists(m_oOpt.Item("-o")) Then
184190
If Not m_oOpt.Item("-q") Then
185-
ConsoleError m_oOpt.Item("-o") & " output successfully!" & vbCrLf & vbCrLf
191+
ConsoleError "File '%1' output successfully!" & vbCrLf, m_oOpt.Item("-o")
186192
End If
187193
End If
188194
QH:
195+
Exit Function
196+
EH:
197+
ConsoleError "Critical error: " & Err.Description & " (0x" & Hex(Err.Number) & ") [Process]" & vbCrLf
198+
Resume QH
189199
End Function
190200

191201
Private Function SplitArgs(sText As String) As Variant
@@ -346,7 +356,7 @@ Private Function EnumFiles( _
346356
Do While LenB(sFile) <> 0
347357
If sFile <> "." And sFile <> ".." Then
348358
sFile = PathCombine(sFolder, sFile)
349-
If (GetAttr(sFile) And eAttrib) = eAttrib Then
359+
If (GetFileAttributes(sFile) And eAttrib + vbVolume) = eAttrib Then
350360
RetVal.Add sFile
351361
End If
352362
End If

0 commit comments

Comments
 (0)