• Stars
    star
    180
  • Rank 213,097 (Top 5 %)
  • Language
    Swift
  • License
    The Unlicense
  • Created almost 6 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Beating `Error.localizedDescription` at its own game.

LegibleError badge-platforms badge-languages badge-ci badge-codecov badge-version

LegibleError’s goal is to prevent you showing the user a string like this:

The operation couldn’t be completed. (ThirdPartyModule.(unknown context at 0xx10d6b4a44).SomeError error 0.)

That string is the default localizedDescription for a Swift Error. Instead use LegibleError and you’ll get something more like this:

The operation couldn’t be completed. (ThirdPartyModule.SomeError.networkFailure(http: 503))

Error.legibleLocalizedDescription

If you have an Error like this:

enum SystemError: Error {
    case databaseFailure(internalCode: Int)
}

let error = SystemError.databaseFailure
// ^^ obviously you’d get this from a callback or `catch` in the real-world

let alert = UIAlertController(…)
alert.message = error.localizedDescription
present(alert)

The alert will show:

The operation couldn’t be completed. (MyModule.(unknown context at 0xx10d6b4a44).SystemError error 0.)

But if we were to use .legibleLocalizedDescription:

import LegibleError

let alert = UIAlertController(…)
alert.message = error.legibleLocalizedDescription
present(alert)

The alert will show:

The operation couldn’t be completed. (SystemError.databaseFailure(internalCode: 34))

Still not great, but way more useful in a bug report.

If you want a great message, implement LocalizedError this will make both localizedDescription and legibleLocalizedDescription return the string you specify:

enum SystemError: LocalizedError {
    case databaseFailure

    var errorDescription: String? {
        switch self {
        case databaseFailure(let code):
            return "A serious database failure occurred. Contact support. (#\(code))"
        }
    }
}

The alert will show:

A serious database failure occurred. Contact support. (#34)


LegibleError exists because:

  1. You have no control over third parties and cannot force them to implement LocalizedError
  2. Some Errors in your codebase are very unlikely and thus “localizing” them is not a good maintenance burden.
  3. When logging errors you want the full information without any risk that the localized version has “fallen behind”, get the compiler to do the work, in such cases use legibleDescription (see the next section).

Loggable Error Descriptions

This:

let msg = "There was an error (\(error))"

Will give you this:

There was an error (databaseFailure)

Which loses the context of the enum’s type; use legibleDescription:

let msg = "There was an error! \(error.legibleDescription)"

There was an error (SystemError.databaseFailure(internalCode: 34))

legibleDescription is to description where legibleLocalizedDescription is to localizedDescription. legibleDescription is always appropriate for communicating to you, the developer, which error happened. Use it in logs and to supplement a good message for the user.

Way better descriptions on Linux

Linux is a little behind, usually you only get The operation could not be completed on Linux. We fully support Linux.

Sponsorship

If you or your company depend on this project, please consider sponsorship so I have justification for maintenance .

Installation

SwiftPM:

package.append(.package(url: "https://github.com/mxcl/LegibleError.git", from: "1.0.0"))

CocoaPods:

pod 'LegibleError', '~> 1.0'

Carthage:

Waiting on: @Carthage#1945.

More Repositories

1

PromiseKit

Promises for Swift & ObjC.
Swift
14,218
star
2

swift-sh

Easily script with third-party Swift dependencies.
Swift
1,732
star
3

Path.swift

Delightful, robust, cross-platform and chainable file-pathing functions.
Swift
929
star
4

Workbench

Seamless, automatic, “dotfile” sync to iCloud.
Swift
668
star
5

YOLOKit

Getting square objects down round holes
Objective-C
663
star
6

Cake

A delicious, quality‑of‑life supplement for your app‑development toolbox.
Swift
537
star
7

OMGHTTPURLRQ

Vital extensions to NSURLRequest that Apple left out for some reason.
Objective-C
339
star
8

Version

semver (Semantic Version) Swift µFramework.
Swift
293
star
9

xcodebuild

A continuously resilient `xcodebuild` “GitHub Action”. Also it’s the best.
TypeScript
285
star
10

AppUpdater

Automatically update open source macOS apps from GitHub releases.
Swift
245
star
11

Chalk

Terminal colors using Swift 5’s string interpolation extensions.
Swift
215
star
12

Audioscrobbler.app

Minimal, robust iTunes scrobbling
Objective-C
140
star
13

UIImageWithColor

[UIImage imageWithColor]
Objective-C
80
star
14

yosemite-menu-inverter

Invert your Yosemite icons
Ruby
71
star
15

MBWebSocketServer

An objc draft 10 websocket implementation
Objective-C
69
star
16

AmA

Ask mxcl anything.
50
star
17

ChuzzleKit

A chuzzled object is nil if it is falsy, otherwise it has its falsy parts removed.
Objective-C
48
star
18

UIImageAverageColor

[UIImage averageColor]
Objective-C
32
star
19

playdar.prefpane

All in one Mac OS X Preference Pane for the Playdar daemon
Objective-C
28
star
20

initWith...FuckIt

Constructors for pithy, potty-mouths.
Objective-C
26
star
21

MBAppStoreRater

Non-intrusive invitation to rate your iOS app
Objective-C
22
star
22

bs

Write HTML inline with your JS
Ruby
22
star
23

UIColorPerceivedLuminance

[UIColor perceivedLuminance]
Objective-C
18
star
24

StreamReader

Swift
17
star
25

SuperGoodDeleteWiggle

Wiggle animation (almost) as good as the Springboard delete wiggle animation
Objective-C
13
star
26

mxcl.github.io

HTML
12
star
27

scrobsub

Portable scrobbling solution in plain c
C
9
star
28

generate-podspec

Generate a podspec for your SwiftPM project based on tagged releases.
TypeScript
9
star
29

Kissogram

Swift
8
star
30

UIInvertedImage

[UIImage invertedImage]
Objective-C
8
star
31

homebrew-made

Ruby
7
star
32

Flickr-Friends.wdgt

A recent photo slideshow of your Flickr Friends for the OS X Dashboard
JavaScript
7
star
33

roofparty

A Last.fm radio player written in D
5
star
34

.github

5
star
35

DraftLottery

Example Swift app
Swift
4
star
36

get-swift-version

JavaScript
4
star
37

Roole.tmBundle

TextMate Syntax Highlighting Bundle for Roole
4
star
38

mxcl

3
star
39

SelfieAssist

Objective-C
3
star
40

MXCLPlaceholderTextView

3
star
41

sym0

Cocoa utility functions with zero symbol pollution
Objective-C
2
star