Class: Util

Inherits:
Object
  • Object
show all
Defined in:
lib/fyipe/util.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Util

Returns a new instance of Util.



6
7
8
# File 'lib/fyipe/util.rb', line 6

def initialize(options)
    @options = options
end

Instance Method Details

#addCodeSnippetToFrame(lines, frame, linesOfContext = 5) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fyipe/util.rb', line 111

def addCodeSnippetToFrame(lines, frame, linesOfContext = 5)

    if lines.length() < 1 
        return
    end

    lineNumber = 0
    if frame['lineNumber'] != nil
        lineNumber = frame['lineNumber'].to_i
    end

    maxLines = lines.length()
    sourceLine = max(min(maxLines, lineNumber - 1), 0)
    # attach the line before the error
    frame['linesBeforeError'] = getPathOfLines(lines, max(0, sourceLine - linesOfContext), linesOfContext)
    # attach the line after the error
    frame['linesAfterError'] = getPathOfLines(
        lines,
        min(sourceLine + 1, maxLines),
        1 + linesOfContext
    )
    # attach the error line
    frame['errorLine'] = lines[min(maxLines - 1, sourceLine)]

    # remove the source file
    frame.delete('sourceFile')

    return frame
end

#getErrorCodeSnippet(errorObj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fyipe/util.rb', line 62

def getErrorCodeSnippet(errorObj)
    frames = [] 
    if errorObj["stacktrace"] != nil
        frames = errorObj["stacktrace"]["frames"]
    end

    # get content related to each frame
    contentFrame = [];

    frames.each do |frame|
        updateFrame = getFrameContent(frame)
        # update content of each frame
        updateFrameContent(frame)
        contentFrame.append(updateFrame)
    end

    errorObj["stacktrace"]["frames"] = frames
    return errorObj
end

#getErrorType(val) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fyipe/util.rb', line 14

def getErrorType(val)
    enumValue = ""
    LogType.each do |key, enum|
        if (key.to_s.eql? "#{val}")
            enumValue = enum.value
        end
    end
    
    return enumValue
end

#getExceptionStackTrace(exception) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fyipe/util.rb', line 24

def getExceptionStackTrace(exception)
    frames = [];
    lineNumber = 'N/A'

    obj = {}
    obj["type"] = exception.class
    obj["message"] = exception.message
    obj["lineNumber"] = lineNumber
    
    backtraces = exception.backtrace
    backtraces != nil ?
    backtraces.each do |backtrace|
        breakdown = backtrace.split(":")
        fileName = breakdown[0]
        lineNumber = breakdown[1]
        methodName = breakdown[2]
        frame = {}
        frame["fileName"] = fileName
        frame["methodName"] = methodName
        frame["lineNumber"] = lineNumber
        frames.append(frame)
    end
    : nil

    stacktrace = {}
    stacktrace["frames"] = frames
    
    obj["stacktrace"] = stacktrace
    obj["lineNumber"] = frames[0] ? frames[0]["lineNumber"] : lineNumber

    # run only if user agreed to use this feature
    if @options[:captureCodeSnippet] == true
        obj = getErrorCodeSnippet(obj)
    end

    return obj
end

#getFrameContent(frame) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fyipe/util.rb', line 82

def getFrameContent(frame)

    fileName = frame['fileName']

    # # try to read the file content and save to frame
    begin
        file = File.open(fileName)
        frame["sourceFile"] = file.readlines.map(&:chomp)

    rescue  => exception
        # something terrible went wrong
        puts "Warning; Could read file: #{exception.message}"      
    ensure
        file.close      
    end
    
    return frame
end

#updateFrameContent(frame) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/fyipe/util.rb', line 101

def updateFrameContent(frame)
    lines = []
    if frame["sourceFile"] != nil
        lines = frame["sourceFile"]
    end
    localFrame = addCodeSnippetToFrame(lines, frame)
    frame = localFrame
    return frame
end

#v4Object



10
11
12
# File 'lib/fyipe/util.rb', line 10

def v4()
    return SecureRandom.uuid
end