karatelabs/karate · error · RuntimeException

missing array argument 'patterns':

Error message

missing array argument 'patterns': 

What it means

Driver.jsGet's INTERCEPT API callable validates the configuration map passed to driver.intercept(). When the map has no 'patterns' key (or it is null), it throws this RuntimeException including the offending config map. The patterns array is mandatory because it defines which URLs the intercept applies to.

Solutions

  1. Add the required patterns array: driver.intercept({ patterns: ['https://example.com/*'], mock: ... })
  2. Check the key spelling — it must be exactly 'patterns'
  3. Verify the value is a (JS) array of URL pattern strings or pattern objects, not a single string or null

Example fix

// before
driver.intercept({ mock: { status: 200, body: 'ok' } });
// after
driver.intercept({ patterns: ['https://api.example.com/data'], mock: { status: 200, body: 'ok' } });
Defensive patterns

Strategy: validation

Validate before calling

// in Karate JS, before calling:
// if (!config.patterns) throw new Error('intercept config requires patterns array');

Type guard

// JS: if (Array.isArray(cfg.patterns) && cfg.patterns.length > 0) { ... }

Try / catch

try { driver.intercept(cfg); } catch (RuntimeException e) { if (e.getMessage().contains("missing array argument 'patterns'")) { /* fix config */ } else throw e; }

Prevention

When it happens

Trigger: driver.intercept({ mock: ... }) without the required 'patterns' array; a config built programmatically where the patterns key was misspelled ('pattern', 'urlPatterns') or set to null.

Common situations: Copy-pasted intercept examples where the patterns entry was dropped; JS object built dynamically and the key name didn't match; mixing up intercept configuration shapes between Karate versions.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/1e0f8ee633d1353d. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/driver/Driver.java:387

            case DriverApi.CLEAR_COOKIES -> (JavaCallable) (ctx, args) -> {
                clearCookies();
                return null;
            };
            case DriverApi.DELETE_COOKIE -> (JavaCallable) (ctx, args) -> {
                deleteCookie(String.valueOf(args[0]));
                return null;
            };

            // Request interception
            // V1 compat:  driver.intercept({ patterns: [...], mock: 'classpath:mock.feature' })
            // V2 handler:  driver.intercept({ patterns: [...], handler: function(req){ return { status: 200, body: '...' } } })
            case DriverApi.INTERCEPT -> (JavaCallable) (ctx, args) -> {
                Object arg = args[0];
                if (arg instanceof Map) {
                    Map<String, Object> configMap = (Map<String, Object>) arg;
                    List<Object> patterns = (List<Object>) configMap.get("patterns");
                    if (patterns == null) {
                        throw new RuntimeException("missing array argument 'patterns': " + configMap);
                    }
                    // Extract URL pattern strings from pattern maps or plain strings
                    List<String> urlPatterns = new java.util.ArrayList<>();
                    for (Object p : patterns) {
                        if (p instanceof Map) {
                            Object urlPattern = ((Map<String, Object>) p).get("urlPattern");
                            urlPatterns.add(urlPattern != null ? String.valueOf(urlPattern) : "*");
                        } else {
                            urlPatterns.add(String.valueOf(p));
                        }
                    }
                    Object handlerObj = configMap.get("handler");
                    String mock = configMap.get("mock") != null ? String.valueOf(configMap.get("mock")) : null;
                    if (handlerObj instanceof JavaCallable jsHandler) {
                        // V2: inline JS handler
                        intercept(urlPatterns, request -> {
                            Object result = jsHandler.call(ctx, request);
                            if (result instanceof InterceptResponse ir) {

View on GitHub (pinned to a22eb90246)