parseWasmError parses a WASM execution error into a user-friendly PolicyError
(err error)
| 103 | |
| 104 | // parseWasmError parses a WASM execution error into a user-friendly PolicyError |
| 105 | func parseWasmError(err error) *PolicyError { |
| 106 | if err == nil { |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | errStr := err.Error() |
| 111 | |
| 112 | // Try pattern matching for known error types |
| 113 | for _, ep := range errorPatterns { |
| 114 | if matches := ep.pattern.FindStringSubmatch(errStr); matches != nil { |
| 115 | message, hint := ep.extract(matches) |
| 116 | return &PolicyError{ |
| 117 | Category: ep.category, |
| 118 | UserMessage: message, |
| 119 | Hint: hint, |
| 120 | OriginalErr: err, |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Fallback: extract first meaningful line before stack trace |
| 126 | lines := strings.Split(errStr, "\n") |
| 127 | for _, line := range lines { |
| 128 | line = strings.TrimSpace(line) |
| 129 | |
| 130 | // Stop at stack trace markers |
| 131 | if line == "" || strings.HasPrefix(line, "wasm stack trace:") || strings.HasPrefix(line, "\t") { |
| 132 | break |
| 133 | } |
| 134 | |
| 135 | // Clean up wazero artifacts |
| 136 | line = strings.ReplaceAll(line, " (recovered by wazero)", "") |
| 137 | line = strings.TrimSpace(line) |
| 138 | |
| 139 | if line != "" { |
| 140 | return &PolicyError{ |
| 141 | Category: CategoryUnknown, |
| 142 | UserMessage: line, |
| 143 | Hint: "Enable debug logging with --debug for more details.", |
| 144 | OriginalErr: err, |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Ultimate fallback |
| 150 | return &PolicyError{ |
| 151 | Category: CategoryUnknown, |
| 152 | UserMessage: "Policy execution failed", |
| 153 | Hint: "Enable debug logging with --debug for detailed error information.", |
| 154 | OriginalErr: err, |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // extractHostname extracts the hostname from a URL string |
| 159 | func extractHostname(urlStr string) string { |