Skip to content

Commit 3765de3

Browse files
newm4nFerdinand Neman
and
Ferdinand Neman
authored
Chore/panic (#398)
* Removed panic in JSON Resouce handling code. Some method signature impacted * removed all panic * Removed panic on json tools related * Removed panic from Working Memory * Removed panic from Working Memory part 2 * Removed panic from JsonDom.go * Worked trying to remove all bad panic --------- Co-authored-by: Ferdinand Neman <ferdinand@hyperjump.tech>
1 parent 332a254 commit 3765de3

12 files changed

+631
-408
lines changed

ast/ExpressionAtom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,5 @@ func (e *ExpressionAtom) Evaluate(dataContext IDataContext, memory *WorkingMemor
413413
return e.Value, nil
414414
}
415415

416-
panic("should not be reached")
416+
return reflect.Value{}, fmt.Errorf("this portion of code should not be reached")
417417
}

ast/KnowledgeBase.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"fmt"
2020
"github.com/google/uuid"
21-
"github.com/sirupsen/logrus"
2221
"io"
2322
"sort"
2423
"strings"
@@ -84,7 +83,6 @@ func (lib *KnowledgeLibrary) LoadKnowledgeBaseFromReader(reader io.Reader, overw
8483
if r := recover(); r != nil {
8584
retKb = nil
8685
retErr = fmt.Errorf("panic recovered during LoadKnowledgeBaseFromReader, recover \"%v\". send us your report to https://github.com/hyperjumptech/grule-rule-engine/issues", r)
87-
logrus.Panicf("panic recovered during LoadKnowledgeBaseFromReader, recover \"%v\". send us your report to https://github.com/hyperjumptech/grule-rule-engine/issues", r)
8886
}
8987
}()
9088

@@ -94,7 +92,10 @@ func (lib *KnowledgeLibrary) LoadKnowledgeBaseFromReader(reader io.Reader, overw
9492

9593
return nil, err
9694
}
97-
knowledgeBase := catalog.BuildKnowledgeBase()
95+
knowledgeBase, err := catalog.BuildKnowledgeBase()
96+
if err != nil {
97+
return nil, err
98+
}
9899
if overwrite {
99100
lib.Library[fmt.Sprintf("%s:%s", knowledgeBase.Name, knowledgeBase.Version)] = knowledgeBase
100101

@@ -127,21 +128,25 @@ func (lib *KnowledgeLibrary) StoreKnowledgeBaseToWriter(writer io.Writer, name,
127128

128129
// NewKnowledgeBaseInstance will create a new instance based on KnowledgeBase blue print
129130
// identified by its name and version
130-
func (lib *KnowledgeLibrary) NewKnowledgeBaseInstance(name, version string) *KnowledgeBase {
131+
func (lib *KnowledgeLibrary) NewKnowledgeBaseInstance(name, version string) (*KnowledgeBase, error) {
131132
knowledgeBase, ok := lib.Library[fmt.Sprintf("%s:%s", name, version)]
132133
if ok {
133-
newClone := knowledgeBase.Clone(pkg.NewCloneTable())
134+
newClone, err := knowledgeBase.Clone(pkg.NewCloneTable())
135+
if err != nil {
136+
return nil, err
137+
}
134138
if knowledgeBase.IsIdentical(newClone) {
135139
AstLog.Debugf("Successfully create instance [%s:%s]", newClone.Name, newClone.Version)
136140

137-
return newClone
141+
return newClone, nil
138142
}
139143
AstLog.Fatalf("ORIGIN : %s", knowledgeBase.GetSnapshot())
140144
AstLog.Fatalf("CLONE : %s", newClone.GetSnapshot())
141-
panic("The clone is not identical")
145+
146+
return nil, fmt.Errorf("the clone is not identical")
142147
}
143148

144-
return nil
149+
return nil, fmt.Errorf("specified knowledge base name and version not exist")
145150
}
146151

147152
// KnowledgeBase is a collection of RuleEntries. It has a name and version.
@@ -210,7 +215,7 @@ func (e *KnowledgeBase) GetSnapshot() string {
210215
}
211216

212217
// Clone will clone this instance of KnowledgeBase and produce another (structure wise) identical instance.
213-
func (e *KnowledgeBase) Clone(cloneTable *pkg.CloneTable) *KnowledgeBase {
218+
func (e *KnowledgeBase) Clone(cloneTable *pkg.CloneTable) (*KnowledgeBase, error) {
214219
clone := &KnowledgeBase{
215220
Name: e.Name,
216221
Version: e.Version,
@@ -228,10 +233,14 @@ func (e *KnowledgeBase) Clone(cloneTable *pkg.CloneTable) *KnowledgeBase {
228233
}
229234
}
230235
if e.WorkingMemory != nil {
231-
clone.WorkingMemory = e.WorkingMemory.Clone(cloneTable)
236+
wm, err := e.WorkingMemory.Clone(cloneTable)
237+
if err != nil {
238+
return nil, err
239+
}
240+
clone.WorkingMemory = wm
232241
}
233242

234-
return clone
243+
return clone, nil
235244
}
236245

237246
// AddRuleEntry add ruleentry into this knowledge base.

ast/RuleEntry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ func (e *RuleEntry) Evaluate(ctx context.Context, dataContext IDataContext, memo
171171
}
172172
defer func() {
173173
if r := recover(); r != nil {
174-
err = fmt.Errorf("Error while evaluating rule %s, panic recovered", e.RuleName)
174+
err = fmt.Errorf("error while evaluating rule %s, panic recovered", e.RuleName)
175+
can = false
175176
}
176177
}()
177178
if e.Retracted {

ast/Serializer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type Catalog struct {
101101
// BuildKnowledgeBase will rebuild a knowledgebase from this Catalog.
102102
// the rebuilt KnowledgeBase is identical to the original KnowledgeBase from
103103
// which this Catalog was built.
104-
func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase {
104+
func (cat *Catalog) BuildKnowledgeBase() (*KnowledgeBase, error) {
105105
workingMem := &WorkingMemory{
106106
Name: cat.MemoryName,
107107
Version: cat.MemoryVersion,
@@ -280,7 +280,7 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase {
280280
}
281281
importTable[amet.AstID] = n
282282
default:
283-
panic("Unrecognized meta type")
283+
return nil, fmt.Errorf("unrecognized meta type")
284284
}
285285
}
286286

@@ -407,7 +407,7 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase {
407407
whenScope.Expression = importTable[amet.ExpressionID].(*Expression)
408408
}
409409
default:
410-
panic("Unrecognized meta type")
410+
return nil, fmt.Errorf("unknown AST type")
411411
}
412412
}
413413

@@ -450,7 +450,7 @@ func (cat *Catalog) BuildKnowledgeBase() *KnowledgeBase {
450450
}
451451
}
452452

453-
return knowledgeBase
453+
return knowledgeBase, nil
454454
}
455455

456456
// Equals used for testing purpose, to ensure that two catalog

ast/WorkingMemory.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (workingMem *WorkingMemory) Equals(that *WorkingMemory) bool {
143143
}
144144

145145
// Clone will clone this WorkingMemory. The new clone will have an identical structure
146-
func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemory {
146+
func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) (*WorkingMemory, error) {
147147
AstLog.Debugf("Cloning working memory %s:%s", workingMem.Name, workingMem.Version)
148148
clone := NewWorkingMemory(workingMem.Name, workingMem.Version)
149149

@@ -154,7 +154,7 @@ func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemor
154154
clone.expressionSnapshotMap[k] = cloneTable.Records[expr.AstID].CloneInstance.(*Expression)
155155
} else {
156156

157-
panic(fmt.Sprintf("expression %s is not on the clone table - %s", expr.GrlText, expr.GetSnapshot()))
157+
return nil, fmt.Errorf("expression %s is not on the clone table - %s", expr.GrlText, expr.GetSnapshot())
158158
}
159159
}
160160
}
@@ -166,7 +166,7 @@ func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemor
166166
clone.expressionAtomSnapshotMap[k] = cloneTable.Records[exprAtm.AstID].CloneInstance.(*ExpressionAtom)
167167
} else {
168168

169-
panic(fmt.Sprintf("expression atom %s is not on the clone table. ASTID %s", exprAtm.GrlText, exprAtm.AstID))
169+
return nil, fmt.Errorf("expression atom %s is not on the clone table. ASTID %s", exprAtm.GrlText, exprAtm.AstID)
170170
}
171171
}
172172
}
@@ -178,7 +178,7 @@ func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemor
178178
clone.variableSnapshotMap[key] = cloneTable.Records[variable.AstID].CloneInstance.(*Variable)
179179
} else {
180180

181-
panic(fmt.Sprintf("variable %s is not on the clone table", variable.GrlText))
181+
return nil, fmt.Errorf("variable %s is not on the clone table", variable.GrlText)
182182
}
183183
}
184184
}
@@ -194,12 +194,12 @@ func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemor
194194
clone.expressionVariableMap[clonedVari][k2] = cloneTable.Records[expr.AstID].CloneInstance.(*Expression)
195195
} else {
196196

197-
panic(fmt.Sprintf("expression %s is not on the clone table", expr.GrlText))
197+
return nil, fmt.Errorf("expression %s is not on the clone table", expr.GrlText)
198198
}
199199
}
200200
} else {
201201

202-
panic(fmt.Sprintf("variable %s is not on the clone table", key.GrlText))
202+
return nil, fmt.Errorf("variable %s is not on the clone table", key.GrlText)
203203
}
204204
}
205205
}
@@ -215,23 +215,23 @@ func (workingMem *WorkingMemory) Clone(cloneTable *pkg.CloneTable) *WorkingMemor
215215
clone.expressionAtomVariableMap[clonedVari][k2] = cloneTable.Records[expr.AstID].CloneInstance.(*ExpressionAtom)
216216
} else {
217217

218-
panic(fmt.Sprintf("expression atom %s is not on the clone table", expr.GrlText))
218+
return nil, fmt.Errorf("expression atom %s is not on the clone table", expr.GrlText)
219219
}
220220
}
221221
} else {
222222

223-
panic(fmt.Sprintf("variable %s is not on the clone table", key.GrlText))
223+
return nil, fmt.Errorf("variable %s is not on the clone table", key.GrlText)
224224
}
225225
}
226226
}
227227

228228
if workingMem.Equals(clone) {
229229
clone.DebugContent()
230230

231-
return clone
231+
return clone, nil
232232
}
233233

234-
panic("Clone not equals the origin.")
234+
return nil, fmt.Errorf("clone not equals the origin")
235235
}
236236

237237
// IndexVariables will index all expression and expression atoms that contains a speciffic variable name

examples/Issue328_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ func TestMethodCall_SliceOOR(t *testing.T) {
4040
err = rb.BuildRuleFromResource("Test", "0.1.1", pkg.NewBytesResource([]byte(SliceOORRule)))
4141
assert.NoError(t, err)
4242

43-
// expect no panic and no error (ReturnErrOnFailedRuleEvaluation = false)
4443
eng1 := &engine.GruleEngine{MaxCycle: 5}
4544
kb := lib.NewKnowledgeBaseInstance("Test", "0.1.1")
4645
err = eng1.Execute(dataContext, kb)
4746
assert.NoError(t, err)
4847

49-
// expect no panic and execute to return an error here
5048
eng1 = &engine.GruleEngine{MaxCycle: 5, ReturnErrOnFailedRuleEvaluation: true}
5149
kb = lib.NewKnowledgeBaseInstance("Test", "0.1.1")
5250
err = eng1.Execute(dataContext, kb)

0 commit comments

Comments
 (0)