Cloud-oriented Life

Cloud Native Technology Improves Lives

ActionView::Helpers::DateHelper

The Date Helper primarily creates select/option tags for different kinds of dates and times or date and time elements. All of the select-type methods share a number of common options that are as follows:

Read more »

request_store-sidekiq

request_store-sidekiq provide an easy integration between RequestStore and Sidekiq.

RequestStore allows you to easily create threadsafe code, and this middleware for Sidekiq brings that functionality to Sidekiq workers, or even ActiveJob backed Sidekiq.

Read more »

errors

Package errors implements functions to manipulate errors.

The New function creates errors whose only content is a text message

The Unwrap, Is and As functions work on errors that may wrap other errors.

Package

Import errors package.

1
import "errors"

func New

1
func New(text string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

1
2
3
4
5
6
7
8
9
10
11
12
    // error message
err := errors.New("error msg")
fmt.Printf("%+v\n", err) // error msg

// format error message
err = errors.New(fmt.Sprintf("error %s", "msg"))
fmt.Printf("%+v\n", err) // error msg

// format error message
err = fmt.Errorf("error %s", "msg")
fmt.Printf("%+v\n", err) // error msg
}

func Unwrap

1
func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err’s type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

If e.Unwrap() returns a non-nil error w, then we say that e wraps w.

A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb to the error argument:

1
2
3
4
5
6
7
8
err := errors.New("error msg")
fmt.Printf("%+v\n", err)

errWrap := fmt.Errorf("%w", err) // Wrapping errors with %w
fmt.Printf("%s %t\n", "err == errWrap", (err == errWrap)) // err == errWrap false

errUnwrap := errors.Unwrap(errWrap)
fmt.Printf("%s %t\n", "err == errUnwrap", (err == errUnwrap)) // err == errUnwrap true

returns err.

func Is

1
func Is(err, target error) bool

Is reports whether any error in err’s chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

An error type might provide an Is method so it can be treated as equivalent to an existing error. For example, if MyError defines

1
func (m MyError) Is(target error) bool { return target == fs.ErrExist }

then Is(MyError{}, fs.ErrExist) returns true. See syscall.Errno.Is for an example in the standard library.

Is unwraps its first argument sequentially looking for an error that matches the second. It reports whether it finds a match. It should be used in preference to simple equality checks:

1
if errors.Is(err, fs.ErrExist)

is preferable to

1
if err == fs.ErrExist

because the former will succeed if err wraps fs.ErrExist.

1
2
3
4
5
6
7
8
9
10
11
12
13
err := errors.New("error msg")
fmt.Printf("%+v\n", err)

errWrap := fmt.Errorf("%w", err) // Wrapping errors with %w
fmt.Printf("%s %t\n", "err == errWrap", (err == errWrap)) // err == errWrap false

errUnwrap := errors.Unwrap(errWrap)
fmt.Printf("%s %t\n", "err == errUnwrap", (err == errUnwrap)) // err == errUnwrap true
fmt.Printf("%s %t\n", "errUnwrap Is err", errors.Is(errUnwrap, err)) // errUnwrap Is err true

errWrapWrap := fmt.Errorf("%w", errWrap)
fmt.Printf("%s %t\n", "err == errWrapWrap", (err == errWrapWrap)) // err == errWrapWrap false
fmt.Printf("%s %t\n", "errWrapWrap Is err", errors.Is(errWrapWrap, err)) // errWrapWrap Is err true

func As

1
func As(err error, target interface{}) bool

As finds the first error in err’s chain that matches target, and if so, sets target to that error value and returns true. Otherwise, it returns false.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error’s concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

An error type might provide an As method so it can be treated as if it were a different error type.

As panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.

1
2
3
4
var perr *fs.PathError
if errors.As(err, &perr) {
fmt.Println(perr.Path)
}

is preferable to

1
2
3
if perr, ok := err.(*fs.PathError); ok {
fmt.Println(perr.Path)
}

because the former will succeed if err wraps an *fs.PathError.

References

[1] errors - The Go Programming Language - https://golang.org/pkg/errors/

[2] Working with Errors in Go 1.13 - The Go Blog - https://blog.golang.org/go1.13-errors

[3] Go by Example: Errors - https://gobyexample.com/errors

Command Query Responsibility Segregation (CQRS)

References

[1] CQRS - https://martinfowler.com/bliki/CQRS.html

[2] What is the CQRS pattern? - Azure Architecture Center | Microsoft Docs - https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs

[3] An illustrated guide to CQRS data patterns | Enable Architect - https://www.redhat.com/architect/illustrated-cqrs

[4] Command Query Responsibility Segregation (CQRS) pattern - IBM Cloud Architecture Center - https://www.ibm.com/cloud/architecture/architectures/event-driven-cqrs-pattern/

[5] CQRS: What? Why? How?. CQRS is a useful pattern to reason… | by Stéphane Derosiaux | Medium - https://sderosiaux.medium.com/cqrs-what-why-how-945543482313

Finance participate in operation and management

So what should finance do to truly participate in operation management? First of all, we must understand what the boss’s basic requirements for finance are?

So, what are the basic financial requirements of most private company bosses?

Read more »

openssl

The problem seems to be that homebrew upgraded their openssl install to 1.1. That version of openssl won’t work with rubies prior to 2.4.x. This problem was probably introduced to your Mac when you upgraded homebrew.

1
/Users/cloudolife/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- openssl (LoadError)
Read more »

Active Record Basics

This guide is an introduction to Active Record.

After reading this guide, you will know:

  • What Object Relational Mapping and Active Record are and how they are used in Rails.

  • How Active Record fits into the Model-View-Controller paradigm.

  • How to use Active Record models to manipulate data stored in a relational database.

  • Active Record schema naming conventions.

  • The concepts of database migrations, validations, and callbacks.

Read more »
0%