♻️ Use defined errors
This commit is contained in:
		
					parent
					
						
							
								5b60305de7
							
						
					
				
			
			
				commit
				
					
						2d68691408
					
				
			
		
					 5 changed files with 34 additions and 20 deletions
				
			
		|  | @ -29,12 +29,12 @@ func (e Entry) getFieldMarshalChan() chan metaRes { | |||
| 	// @todo figure out a way to handle json field | ||||
| 	for i := 0; i < size; i++ { | ||||
| 		wg.Add(1) | ||||
| 		go func(i int) { | ||||
| 		go func(m Meta) { | ||||
| 			defer wg.Done() | ||||
| 			o, er := e.Fields[i].MarshalText() | ||||
| 			o, er := m.MarshalText() | ||||
| 			ch <- metaRes{o, er} | ||||
| 
 | ||||
| 		}(i) | ||||
| 		}(e.Fields[i]) | ||||
| 	} | ||||
| 
 | ||||
| 	go func() { | ||||
|  | @ -47,10 +47,10 @@ func (e Entry) getFieldMarshalChan() chan metaRes { | |||
| func (e Entry) MarshalText() ([]byte, error) { | ||||
| 	e.Title = strings.TrimSpace(e.Title) | ||||
| 	if e.Title == "" { | ||||
| 		return []byte{}, errors.New("Empty title") | ||||
| 		return []byte{}, ErrorMissingTitle | ||||
| 	} | ||||
| 	if e.Date == (time.Time{}) { | ||||
| 		return []byte{}, errors.New("Empty date") | ||||
| 		return []byte{}, ErrorMissingDate | ||||
| 	} | ||||
| 	ch := e.getFieldMarshalChan() | ||||
| 	buff := &bytes.Buffer{} | ||||
|  |  | |||
|  | @ -28,8 +28,8 @@ func TestEntryMarshal(t *testing.T) { | |||
| 		lines  []string | ||||
| 		err    error | ||||
| 	}{ | ||||
| 		{"no-title", "", when, simple, "", nolines, errors.New("Empty title")}, | ||||
| 		{"zero-date", "Empty title", time.Time{}, simple, "", nolines, errors.New("Empty date")}, | ||||
| 		{"no-title", "", when, simple, "", nolines, ErrorMissingTitle}, | ||||
| 		{"zero-date", "Empty title", time.Time{}, simple, "", nolines, ErrorMissingDate}, | ||||
| 		{"one-line", "A Title", when, simple, "@begin " + whens + " - A Title @end", nolines, nil}, | ||||
| 		{ | ||||
| 			"one-field", | ||||
|  |  | |||
							
								
								
									
										14
									
								
								models/errors.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								models/errors.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | |||
| package models | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| ) | ||||
| 
 | ||||
| var ErrorMissingTitle = errors.New("Missing title") | ||||
| var ErrorMissingDate = errors.New("Missing date") | ||||
| var ErrorParsing = errors.New("Parsing Error") | ||||
| 
 | ||||
| func newParsingError(err error) error { | ||||
| 	return fmt.Errorf("%w: %w", ErrorParsing, err) | ||||
| } | ||||
|  | @ -56,12 +56,12 @@ func (m Meta) MarshalText() ([]byte, error) { | |||
| 
 | ||||
| func (m *Meta) UnmarshalText(in []byte) error { | ||||
| 	if len(in) == 0 { | ||||
| 		return errors.New("Unable to Unmarshal empty string") | ||||
| 		return newParsingError(errors.New("Unable to Unmarshal empty string")) | ||||
| 	} | ||||
| 	re := regexp.MustCompile("(?s)^@([^ ]+) (.*)( @end)?$") | ||||
| 	match := re.FindSubmatch(in) | ||||
| 	if len(match) == 0 { | ||||
| 		return fmt.Errorf("Failed to match %s", in) | ||||
| 		return newParsingError(fmt.Errorf("Failed to match %s", in)) | ||||
| 	} | ||||
| 	m.Key = string(match[1]) | ||||
| 	return m.processMeta(match[2]) | ||||
|  | @ -69,11 +69,11 @@ func (m *Meta) UnmarshalText(in []byte) error { | |||
| 
 | ||||
| func (m *Meta) processMeta(in []byte) error { | ||||
| 	if len(in) == 0 { | ||||
| 		return errors.New("No value found") | ||||
| 		return newParsingError(errors.New("No value found")) | ||||
| 	} | ||||
| 	s := strings.TrimSpace(string(in)) | ||||
| 	if len(s) == 0 { | ||||
| 		return errors.New("No value found") | ||||
| 		return newParsingError(errors.New("No value found")) | ||||
| 	} | ||||
| 	yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$") | ||||
| 	yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$") | ||||
|  |  | |||
|  | @ -32,7 +32,7 @@ func TestMeta(t *testing.T) { | |||
| 		{"json number", "num", json.Number("42.13"), "@num 42.13", nil, 42.13}, | ||||
| 		{"true", "b", true, "@b true", nil, true}, | ||||
| 		{"false", "b", false, "@b false", nil, false}, | ||||
| 		{"nil", "n", nil, "", nil, errors.New("Unable to Unmarshal empty string")}, | ||||
| 		{"nil", "n", nil, "", nil, ErrorParsing}, | ||||
| 		{"time", "when", when, "@when " + when.Format(time.RFC3339), nil, when}, | ||||
| 		{"rune", "char", '@', "@char @", nil, "@"}, | ||||
| 		{"bytes", "byteme", []byte("yo"), "@byteme yo", nil, "yo"}, | ||||
|  | @ -41,11 +41,11 @@ func TestMeta(t *testing.T) { | |||
| 		{"json-arr", "arr", json.RawMessage(`["foo",42,"bar", null,"quux", true]`), `@arr ["foo",42,"bar", null,"quux", true]`, nil, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, | ||||
| 		{"chan", "nope", make(chan bool), "", errors.New("Unknown type chan bool"), ""}, | ||||
| 		{"whitespace-key", "no space", "hi", "", errors.New("whitespace is not allowed in key: no space"), ""}, | ||||
| 		{"empty-mar", "nope", skipMarshalTest, "", nil, errors.New("Unable to Unmarshal empty string")}, | ||||
| 		{"no-key-mar", "nope", skipMarshalTest, "nope", nil, errors.New("Failed to match nope")}, | ||||
| 		{"no-value-mar", "nope", skipMarshalTest, "@nope ", nil, errors.New("No value found")}, | ||||
| 		{"space-value-mar", "nope", skipMarshalTest, "@nope  ", nil, errors.New("No value found")}, | ||||
| 		{"space-value-mar", "nope", skipMarshalTest, "@nope  \n  ", nil, errors.New("No value found")}, | ||||
| 		{"empty-mar", "nope", skipMarshalTest, "", nil, ErrorParsing}, | ||||
| 		{"no-key-mar", "nope", skipMarshalTest, "nope", nil, ErrorParsing}, | ||||
| 		{"no-value-mar", "nope", skipMarshalTest, "@nope ", nil, ErrorParsing}, | ||||
| 		{"space-value-mar", "nope", skipMarshalTest, "@nope  ", nil, ErrorParsing}, | ||||
| 		{"space-nl-value-mar", "nope", skipMarshalTest, "@nope  \n  ", nil, ErrorParsing}, | ||||
| 		{"null-value-mar", "nope", skipMarshalTest, "@nope null", nil, nil}, | ||||
| 		{"tilda-value-mar", "nope", skipMarshalTest, "@nope ~", nil, nil}, | ||||
| 		{"none-value-mar", "nope", skipMarshalTest, "@nope none", nil, nil}, | ||||
|  | @ -81,7 +81,7 @@ func getMetaTestRunner(key string, value any, out string, err error, newVal any) | |||
| 			e = n.UnmarshalText([]byte(out)) | ||||
| 		} | ||||
| 		if newE, ok := newVal.(error); ok { | ||||
| 			assert.Equal(t, newE, e) | ||||
| 			assert.ErrorIs(t, e, newE) | ||||
| 		} else { | ||||
| 			assert.Equal(t, key, n.Key) | ||||
| 			if ti, ok := newVal.(time.Time); ok { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue