Skip to content

Commit 5626109

Browse files
committed
Updated SDLError
1 parent ec16d2b commit 5626109

File tree

11 files changed

+95
-40
lines changed

11 files changed

+95
-40
lines changed

Sources/SDL/DisplayMode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public extension SDLDisplayMode {
5151
init(display: SDLVideoDisplay, index: SDLDisplayMode.Index) throws {
5252

5353
var internalValue = SDL_DisplayMode()
54-
try SDL_GetDisplayMode(Int32(display.rawValue), Int32(index.rawValue), &internalValue).sdlThrow()
54+
try SDL_GetDisplayMode(Int32(display.rawValue), Int32(index.rawValue), &internalValue).sdlThrow(type: type(of: self))
5555
self.init(internalValue)
5656
}
5757
}

Sources/SDL/Error.swift

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,82 @@
88
import CSDL2
99

1010
/// SDL Error
11-
public struct SDLError: CustomStringConvertible, Error {
11+
public struct SDLError: Error {
1212

13-
public let description: String
13+
public let errorMessage: String
14+
15+
internal let debugInformation: DebugInformation?
16+
}
17+
18+
extension SDLError: CustomStringConvertible {
19+
20+
public var description: String {
21+
22+
return errorMessage
23+
}
24+
}
25+
26+
internal extension SDLError {
27+
28+
final class DebugInformation: CustomStringConvertible {
29+
30+
public let file: String
31+
32+
public let type: String
33+
34+
public let function: String
35+
36+
public let line: UInt
37+
38+
internal init(file: String,
39+
type: Any,
40+
function: String,
41+
line: UInt) {
42+
43+
self.file = file
44+
self.function = function
45+
self.line = line
46+
self.type = String(reflecting: type)
47+
}
48+
49+
public lazy var description: String = {
50+
51+
return [file, line.description, type, function]
52+
.compactMap { $0 }
53+
.reduce("") { $0 + ($0.isEmpty ? "" : ":") + $1 }
54+
}()
55+
}
1456
}
1557

1658
internal extension SDLError {
1759

1860
/// Text for last reported error.
19-
static var current: SDLError? {
61+
static func current(debugInformation: DebugInformation? = nil) -> SDLError? {
2062

2163
guard let cString = SDL_GetError()
2264
else { return nil }
2365

2466
SDL_ClearError() // reset error
2567
let errorDescription = String(cString: cString)
26-
return SDLError(description: errorDescription)
68+
69+
return SDLError(errorMessage: errorDescription, debugInformation: debugInformation)
2770
}
2871
}
2972

3073
internal extension CInt {
3174

3275
/// Throws for error codes.
3376
@inline(__always)
34-
func sdlThrow() throws {
77+
func sdlThrow(file: String = #file,
78+
type: Any,
79+
function: String = #function,
80+
line: UInt = #line) throws {
3581

3682
guard self >= 0 else {
37-
guard let error = SDLError.current else {
83+
let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line)
84+
guard let error = SDLError.current(debugInformation: debugInformation) else {
3885
assertionFailure("No error for error code \(self)")
39-
return
86+
throw SDLError(errorMessage: "Error code \(self)", debugInformation: debugInformation)
4087
}
4188
throw error
4289
}
@@ -47,12 +94,16 @@ internal extension Optional {
4794

4895
/// Unwraps optional value, throwing error if nil.
4996
@inline(__always)
50-
func sdlThrow() throws -> Wrapped {
97+
func sdlThrow(file: String = #file,
98+
type: Any,
99+
function: String = #function,
100+
line: UInt = #line) throws -> Wrapped {
51101

52102
guard let value = self else {
53-
guard let error = SDLError.current else {
103+
let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line)
104+
guard let error = SDLError.current(debugInformation: debugInformation) else {
54105
assertionFailure("No error for nil value \(Wrapped.self)")
55-
throw SDLError(description: "")
106+
throw SDLError(errorMessage: "Nil value \(Wrapped.self)", debugInformation: debugInformation)
56107
}
57108
throw error
58109
}

Sources/SDL/Palette.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class SDLPalette {
2424
public init(numberOfColors: Int) throws {
2525

2626
let internalFormat = SDL_AllocPalette(Int32(numberOfColors))
27-
self.internalPointer = try internalFormat.sdlThrow()
27+
self.internalPointer = try internalFormat.sdlThrow(type: type(of: self))
2828
}
2929

3030
// MARK: - Accessors

Sources/SDL/PixelFormat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class SDLPixelFormat {
2626
public init(format: SDLPixelFormat.Format) throws {
2727

2828
let internalFormat = SDL_AllocFormat(format.rawValue)
29-
self.internalPointer = try internalFormat.sdlThrow()
29+
self.internalPointer = try internalFormat.sdlThrow(type: type(of: self))
3030
}
3131

3232
// MARK: - Accessors
@@ -42,7 +42,7 @@ public final class SDLPixelFormat {
4242
/// Set the palette for a pixel format structure
4343
public func setPalette(_ palette: SDLPalette) throws {
4444

45-
try SDL_SetPixelFormatPalette(internalPointer, palette.internalPointer).sdlThrow()
45+
try SDL_SetPixelFormatPalette(internalPointer, palette.internalPointer).sdlThrow(type: type(of: self))
4646
}
4747
}
4848

Sources/SDL/Renderer.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class SDLRenderer {
2626
options: BitMaskOptionSet<SDLRenderer.Option> = []) throws {
2727

2828
let internalPointer = SDL_CreateRenderer(window.internalPointer, Int32(driver.rawValue), options.rawValue)
29-
self.internalPointer = try internalPointer.sdlThrow()
29+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
3030
}
3131

3232
/// The color used for drawing operations (Rect, Line and Clear).
@@ -37,15 +37,15 @@ public final class SDLRenderer {
3737
var blue: UInt8 = 0
3838
var alpha: UInt8 = 0
3939

40-
try SDL_GetRenderDrawColor(internalPointer, &red, &green, &blue, &alpha).sdlThrow()
40+
try SDL_GetRenderDrawColor(internalPointer, &red, &green, &blue, &alpha).sdlThrow(type: type(of: self))
4141

4242
return (red, green, blue, alpha)
4343
}
4444

4545
/// Set the color used for drawing operations (Rect, Line and Clear).
4646
public func setDrawColor(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8 = .max) throws {
4747

48-
try SDL_SetRenderDrawColor(internalPointer, red, green, blue, alpha).sdlThrow()
48+
try SDL_SetRenderDrawColor(internalPointer, red, green, blue, alpha).sdlThrow(type: type(of: self))
4949
}
5050

5151
/// Current rendering target texture.
@@ -54,7 +54,7 @@ public final class SDLRenderer {
5454
/// Set a texture as the current rendering target.
5555
public func setTarget(_ newValue: SDLTexture?) throws {
5656

57-
try SDL_SetRenderTarget(internalPointer, target?.internalPointer).sdlThrow()
57+
try SDL_SetRenderTarget(internalPointer, target?.internalPointer).sdlThrow(type: type(of: self))
5858

5959
// hold reference
6060
self.target = newValue
@@ -73,7 +73,7 @@ public final class SDLRenderer {
7373
/// - Note: If the blend mode is not supported, the closest supported mode is chosen.
7474
public func setDrawBlendMode(_ newValue: BitMaskOptionSet<SDLBlendMode>) throws {
7575

76-
try SDL_SetRenderDrawBlendMode(internalPointer, SDL_BlendMode(newValue.rawValue)).sdlThrow()
76+
try SDL_SetRenderDrawBlendMode(internalPointer, SDL_BlendMode(newValue.rawValue)).sdlThrow(type: type(of: self))
7777
}
7878

7979
// MARK: - Methods
@@ -82,7 +82,7 @@ public final class SDLRenderer {
8282
/// This function clears the entire rendering target, ignoring the viewport.
8383
public func clear() throws {
8484

85-
try SDL_RenderClear(internalPointer).sdlThrow()
85+
try SDL_RenderClear(internalPointer).sdlThrow(type: type(of: self))
8686
}
8787

8888
/// Update the screen with rendering performed.
@@ -110,7 +110,7 @@ public final class SDLRenderer {
110110
destinationPointer = nil
111111
}
112112

113-
try SDL_RenderCopy(internalPointer, texture.internalPointer, sourcePointer, destinationPointer).sdlThrow()
113+
try SDL_RenderCopy(internalPointer, texture.internalPointer, sourcePointer, destinationPointer).sdlThrow(type: type(of: self))
114114
}
115115

116116
/// Fill a rectangle on the current rendering target with the drawing color.
@@ -123,7 +123,7 @@ public final class SDLRenderer {
123123
rectPointer = nil
124124
}
125125

126-
try SDL_RenderFillRect(internalPointer, rectPointer).sdlThrow()
126+
try SDL_RenderFillRect(internalPointer, rectPointer).sdlThrow(type: type(of: self))
127127
}
128128
}
129129

@@ -170,7 +170,7 @@ public extension SDLRenderer {
170170

171171
// get driver info from SDL
172172
var info = SDL_RendererInfo()
173-
try SDL_GetRenderDriverInfo(Int32(driver.rawValue), &info).sdlThrow()
173+
try SDL_GetRenderDriverInfo(Int32(driver.rawValue), &info).sdlThrow(type: type(of: self))
174174

175175
self.init(info)
176176
}

Sources/SDL/SDL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public struct SDL {
99
/// - Note: This must be called before using most other SDL functions.
1010
public static func initialize(subSystems: BitMaskOptionSet<SubSystem>) throws {
1111

12-
try SDL_Init(subSystems.rawValue).sdlThrow()
12+
try SDL_Init(subSystems.rawValue).sdlThrow(type: type(of: self))
1313
}
1414

1515
/// Cleans up all initialized subsystems.

Sources/SDL/Surface.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class SDLSurface {
2727

2828
let internalPointer = SDL_CreateRGBSurface(0, CInt(size.width), CInt(size.height), CInt(depth), CUnsignedInt(mask.red), CUnsignedInt(mask.green), CUnsignedInt(mask.blue), CUnsignedInt(mask.alpha))
2929

30-
self.internalPointer = try internalPointer.sdlThrow()
30+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
3131
}
3232

3333
// Get the SDL surface associated with the window.
@@ -39,7 +39,7 @@ public final class SDLSurface {
3939
public init(window: SDLWindow) throws {
4040

4141
let internalPointer = SDL_GetWindowSurface(window.internalPointer)
42-
self.internalPointer = try internalPointer.sdlThrow()
42+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
4343
}
4444

4545
// MARK: - Accessors
@@ -101,7 +101,7 @@ public final class SDLSurface {
101101
/// as critical system locks may be held during this time.
102102
internal func lock() throws {
103103

104-
try SDL_LockSurface(internalPointer).sdlThrow()
104+
try SDL_LockSurface(internalPointer).sdlThrow(type: type(of: self))
105105
}
106106

107107
internal func unlock() {
@@ -112,7 +112,7 @@ public final class SDLSurface {
112112
public func blit(to surface: SDLSurface, source: SDL_Rect? = nil, destination: SDL_Rect? = nil) throws {
113113

114114
// TODO rects
115-
try SDL_UpperBlit(internalPointer, nil, surface.internalPointer, nil).sdlThrow()
115+
try SDL_UpperBlit(internalPointer, nil, surface.internalPointer, nil).sdlThrow(type: type(of: self))
116116
}
117117

118118
public func fill(rect: SDL_Rect? = nil, color: UInt32) throws {
@@ -124,6 +124,6 @@ public final class SDLSurface {
124124
rectPointer = nil
125125
}
126126

127-
try SDL_FillRect(internalPointer, rectPointer, color).sdlThrow()
127+
try SDL_FillRect(internalPointer, rectPointer, color).sdlThrow(type: type(of: self))
128128
}
129129
}

Sources/SDL/Texture.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class SDLTexture {
3737
Int32(width),
3838
Int32(height))
3939

40-
self.internalPointer = try internalPointer.sdlThrow()
40+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
4141
}
4242

4343
/// Create a texture from an existing surface.
@@ -47,7 +47,7 @@ public final class SDLTexture {
4747
public init(renderer: SDLRenderer, surface: SDLSurface) throws {
4848

4949
let internalPointer = SDL_CreateTextureFromSurface(renderer.internalPointer, surface.internalPointer)
50-
self.internalPointer = try internalPointer.sdlThrow()
50+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
5151
}
5252

5353
// MARK: - Accessors
@@ -59,7 +59,7 @@ public final class SDLTexture {
5959
var width = Int32()
6060
var height = Int32()
6161

62-
try SDL_QueryTexture(internalPointer, &format, &access, &width, &height).sdlThrow()
62+
try SDL_QueryTexture(internalPointer, &format, &access, &width, &height).sdlThrow(type: type(of: self))
6363

6464
return Attributes(format: SDLPixelFormat.Format(rawValue: format),
6565
access: SDLTexture.Access(rawValue: access)!,
@@ -71,14 +71,14 @@ public final class SDLTexture {
7171
public func blendMode() throws -> BitMaskOptionSet<SDLBlendMode> {
7272

7373
var value = SDL_BlendMode(0)
74-
try SDL_GetTextureBlendMode(internalPointer, &value).sdlThrow()
74+
try SDL_GetTextureBlendMode(internalPointer, &value).sdlThrow(type: type(of: self))
7575
return BitMaskOptionSet<SDLBlendMode>(rawValue: value.rawValue)
7676
}
7777

7878
/// Set the blend mode used for texture copy operations.
7979
public func setBlendMode(_ newValue: BitMaskOptionSet<SDLBlendMode>) throws {
8080

81-
try SDL_SetTextureBlendMode(internalPointer, SDL_BlendMode(newValue.rawValue)).sdlThrow()
81+
try SDL_SetTextureBlendMode(internalPointer, SDL_BlendMode(newValue.rawValue)).sdlThrow(type: type(of: self))
8282
}
8383

8484
// MARK: - Methods
@@ -113,7 +113,7 @@ public final class SDLTexture {
113113
var pixels: UnsafeMutableRawPointer? = nil
114114

115115
/// must be SDL_TEXTUREACCESS_STREAMING or throws
116-
try SDL_LockTexture(internalPointer, rectPointer, &pixels, &pitch).sdlThrow()
116+
try SDL_LockTexture(internalPointer, rectPointer, &pixels, &pitch).sdlThrow(type: type(of: self))
117117

118118
defer { SDL_UnlockTexture(internalPointer) }
119119

Sources/SDL/VideoDisplay.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public extension SDLVideoDisplay {
5454
let count = SDL_GetNumDisplayModes(Int32(rawValue))
5555

5656
// make sure value is valid
57-
try count.sdlThrow()
57+
try count.sdlThrow(type: type(of: self))
5858

5959
let set = CountableSet<SDLDisplayMode.Index>(count: Int(count))
6060

Sources/SDL/Window.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class SDLWindow {
2727

2828
let internalPointer = SDL_CreateWindow(title, frame.x.rawValue, frame.y.rawValue, Int32(frame.width), Int32(frame.height), options.rawValue)
2929

30-
self.internalPointer = try internalPointer.sdlThrow()
30+
self.internalPointer = try internalPointer.sdlThrow(type: type(of: self))
3131
}
3232

3333
// MARK: - Accessors
@@ -41,7 +41,7 @@ public final class SDLWindow {
4141
public func displayMode() throws -> SDLDisplayMode {
4242

4343
var sdlDisplayMode = SDL_DisplayMode()
44-
try SDL_GetWindowDisplayMode(internalPointer, &sdlDisplayMode).sdlThrow()
44+
try SDL_GetWindowDisplayMode(internalPointer, &sdlDisplayMode).sdlThrow(type: type(of: self))
4545
return SDLDisplayMode(sdlDisplayMode)
4646
}
4747

@@ -89,7 +89,7 @@ public final class SDLWindow {
8989
/// Copy the window surface to the screen.
9090
public func updateSurface() throws {
9191

92-
try SDL_UpdateWindowSurface(internalPointer).sdlThrow()
92+
try SDL_UpdateWindowSurface(internalPointer).sdlThrow(type: type(of: self))
9393
}
9494

9595
/// Set the display mode to use when a window is visible at fullscreen.

Sources/SDLDemo/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@ func main() throws {
107107
}
108108

109109
do { try main() }
110-
catch { fatalError("SDL failed: \(error)") }
110+
catch {
111+
print("Error: \(error)")
112+
dump(error)
113+
exit(EXIT_FAILURE)
114+
}

0 commit comments

Comments
 (0)