Last active
February 2, 2022 10:44
-
-
Save isabellachen/0d42a4d46e432117b9b416dfe387c21a to your computer and use it in GitHub Desktop.
TypeScript Series: Type Checking Errors: Handle errors with type unknown | Type check with classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/a/64620472/2835005 | |
// https://www.typescript-training.com/course/fundamentals-v3/11-top-and-bottom-types/ | |
let unknownError: unknown = {message: 'an error occured'} | |
interface AxiosError { | |
message: string | |
} | |
function isAxiosError(unknownError: unknown): unknownError is AxiosError { | |
if ((unknownError as AxiosError).message) { | |
return true | |
} | |
return false | |
} | |
function returnErrorMessage(unknownError: unknown): string { | |
if (isAxiosError(unknownError)) { | |
return unknownError.message | |
} | |
return 'Unknown Server Error' | |
} | |
console.log(returnErrorMessage(unknownError)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function someErrorThrown(): any { | |
return {} as any | |
} | |
let unknownError: unknown = {message: 'an error occured'} | |
class ApiError { | |
constructor(public status: number, public message: string) { | |
this.status = status | |
this.message = message | |
} | |
} | |
class EngineError { | |
constructor(public status: number, public id: string) { | |
this.status = status | |
this.id = id | |
} | |
} | |
type ServerError = ApiError | EngineError | |
const someError: ServerError = someErrorThrown() | |
function processError(error: unknown) { | |
if (error instanceof ApiError) { | |
console.log('error message is ',error.message) | |
return | |
} | |
if (error instanceof EngineError) { | |
console.log('error id is ',error.id) | |
return | |
} | |
} | |
function throwARandomError() { | |
throw new Error() | |
} | |
const apiError = new ApiError(500, 'Server Error') | |
processError(apiError) | |
const engineError = new EngineError(502, 'Something went horribly wrong') | |
processError(engineError) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @errors: 2345 | |
function obtainRandomVehicle(): any { | |
return {} as any | |
} | |
class Car { | |
drive() { | |
console.log("vroom") | |
} | |
} | |
class Truck { | |
tow() { | |
console.log("dragging something") | |
} | |
} | |
class Boat { | |
isFloating() { | |
return true | |
} | |
} | |
type Vehicle = Truck | Car | Boat | |
let myVehicle: Vehicle = obtainRandomVehicle() | |
class UnreachableError extends Error { | |
constructor(_nvr: never, message: string) { | |
super(message) | |
} | |
} | |
if (myVehicle instanceof Truck) { | |
myVehicle.tow() | |
} else if (myVehicle instanceof Car) { | |
myVehicle.drive() | |
} else { | |
throw new UnreachableError( | |
myVehicle, `unexpected vehicle type ${myVehicle}` | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment