errors


Node.js

  1. const err1 = new Error('some error')
  2. console.log(err1)
  3. class FooError extends Error{
  4. constructor(message) {
  5. super(message)
  6. this.name = 'FooError'
  7. this.message = message
  8. }
  9. toString() {
  10. return this.message
  11. }
  12. }
  13. const err2 = new FooError('my custom error')
  14. console.log(err2)

Output

  1. Error: some error
  2. { FooError: my custom error }

Go

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type FooError struct {
  7. s string
  8. }
  9. func (f *FooError) Error() string {
  10. return f.s
  11. }
  12. func NewFooError(s string) error {
  13. return &FooError{s}
  14. }
  15. func main() {
  16. err1 := errors.New("some error")
  17. fmt.Println(err1)
  18. err2 := NewFooError("my custom error")
  19. fmt.Println(err2)
  20. }

Output

  1. some error
  2. my custom error