Skip to content

Instantly share code, notes, and snippets.

@ivankisyov
Last active April 7, 2019 16:34
Show Gist options
  • Save ivankisyov/7126ca29e53316336ebb21ef8e70c79d to your computer and use it in GitHub Desktop.
Save ivankisyov/7126ca29e53316336ebb21ef8e70c79d to your computer and use it in GitHub Desktop.
[Wiki] Errors

Errors

Types of errors

  • unexpected
    • server offline
    • unhandled exceptions
    • network is down
  • expected
    • 400 - Bad Request
    • 404 - Not Found

Make sure you handle those types accordingly(by status code checks, etc.)

Handling HTTP errors in Angular

Make sure you've already set up a service for dealing with data.
Use the catch operator to return new observable which contains the error

...
Obervable.throw(...{type of error})
...

type of error - specific to our application domain
which means, create new class which represents app specific errors

class AppError {
  constructor(public originalError?: any) {}
}

Create specific app domain errors which inherit from the general app error

class NotFoundError extends AppError {}

Service level - check the status and return new Observable based on the type of app domain error
Component level - check if the error is an instance of specific type of app domain class error and act accordingly

Angular does provide global error handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment