All files / src/rules unhandled_classic_exception.ts

98.6% Statements 282/286
94.05% Branches 95/101
100% Functions 12/12
98.6% Lines 282/286

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 2871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42256x 42256x 42256x 42256x 42256x 42256x 42256x 42256x 42256x 42256x 42256x 1x 1x 1x 1x 1x 1x 21145x 21145x 21145x 21145x 21145x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 21145x 21145x 421x 421x 421x 421x 21145x 21145x 20205x 20205x 21145x 21145x 387x 387x 21145x 21145x 433x 34x 34x 399x 399x 433x 9x 9x 390x 390x 390x 390x 433x 4408x 30x 30x 30x 4378x 4408x 19x 19x 19x 19x 3x 3x 3x 3x 3x 3x 3x 3x 19x 4378x 4408x 76x 76x     76x 76x 76x 76x 13x 13x 76x 4378x 390x 390x 390x 21145x 21145x 30x 30x 30x 5x 5x 25x 25x 30x     25x 25x 25x 30x 23x 23x 23x 10x 10x 10x 10x 10x 10x 23x 25x 25x 25x 25x 25x 25x 30x 6x 6x 25x 30x 7x 5x 7x 7x 5x 5x 5x 5x 7x 25x 25x 25x 21145x 21145x 104x 104x 104x 3x 3x 101x 104x 104x 78x 78x 78x 78x 64x 64x 14x 14x 14x 23x 104x 7x 7x 16x 16x 16x 104x 4x 4x 12x 104x 104x 104x 4x 4x 4x 8x 8x 104x 2x 2x 8x 104x 4x 4x 4x 4x 4x 21145x 21145x 21145x 79x 2x 2x 77x 77x 79x 79x 79x 21145x 21145x 78x 78x 4x 4x 74x 74x 74x 74x 74x 21145x 21145x 23x 23x 23x 10x 10x 10x 10x 23x 23x 23x 21145x 21145x  
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Severity} from "../severity";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {FunctionGroup} from "../objects";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {MethodDefinition} from "../abap/types";
import {FunctionModuleDefinition} from "../abap/types/function_module_definition";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {methodReferences, positionKey} from "./_type_lookup";
 
export class UnhandledClassicExceptionConf extends BasicRuleConfig {
  public severity?: Severity = Severity.Warning;
  /** Report an EXCEPTIONS list without OTHERS, unless the method or function module is known and all its exceptions are listed */
  public missingOthers: boolean = true;
  /** Report a call without EXCEPTIONS, including a functional call, of a method or function module that declares classic exceptions */
  public missingExceptions: boolean = true;
  /** missingExceptions: do not report a method call whose arguments are all literals or me, like describe_by_name( 'T100' ) */
  public allowLiteralArguments: boolean = true;
  /** Report CALL FUNCTION without error_message, the policy "always list error_message", for code that runs outside dialog */
  public missingErrorMessage: boolean = false;
}
 
interface ICallee {
  name: string;
  exceptions: string[];
}
 
export class UnhandledClassicException extends ABAPRule {
  private conf = new UnhandledClassicExceptionConf();
  private functionModules: Map<string, FunctionModuleDefinition> | undefined;
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "unhandled_classic_exception",
      title: "Unhandled classic exception",
      shortDescription: `Finds calls that do not map every classic exception the callee can raise`,
      extendedInformation: `A classic exception that is raised with RAISE and not mapped in the EXCEPTIONS addition of the call
ends in the runtime error RAISE_EXCEPTION. TRY ... CATCH cx_root does not catch it.
 
missingOthers: reports CALL FUNCTION and method calls with an EXCEPTIONS list without OTHERS.
Not reported if the function module or method is known and every exception it declares is listed,
for CALL FUNCTION ... DESTINATION this includes SYSTEM_FAILURE and COMMUNICATION_FAILURE.
CALL FUNCTION ... STARTING NEW TASK, IN UPDATE TASK and IN BACKGROUND are not checked.
 
missingExceptions: reports calls without EXCEPTIONS of a method or function module that declares classic exceptions,
this includes functional calls, which cannot have EXCEPTIONS.
With allowLiteralArguments, a method call whose arguments are all literals or me is not reported.
 
missingErrorMessage, off by default: reports every CALL FUNCTION that does not list the predefined exception error_message,
the policy "always list error_message". Any function module can send a MESSAGE without RAISING, directly or from a
function module it calls, and OTHERS does not catch it. Outside dialog, in HTTP handlers, RFC and background jobs,
a message of type E or A ends the processing. Use it together with check_subrc, otherwise error_message turns a
visible abort into a silent failure. Not reported: DESTINATION, STARTING NEW TASK, IN UPDATE TASK, IN BACKGROUND,
EXCEPTION-TABLE, and function modules in the registry with class-based exceptions.
 
Signatures are taken from the registry, including dependencies. Methods and function modules that are not
found (void) are only checked by missingOthers.
 
Does not report any issues if the code contains syntax errors`,
      tags: [RuleTag.Syntax],
      badExample: `CALL FUNCTION 'Z_READ'
  EXCEPTIONS
    not_found = 1.
 
lo_type = cl_abap_typedescr=>describe_by_name( lv_name ).`,
      goodExample: `CALL FUNCTION 'Z_READ'
  EXCEPTIONS
    not_found = 1
    OTHERS    = 2.
 
cl_abap_typedescr=>describe_by_name(
  EXPORTING
    p_name         = lv_name
  RECEIVING
    p_descr_ref    = lo_type
  EXCEPTIONS
    type_not_found = 1
    OTHERS         = 2 ).`,
    };
  }
 
  public initialize(reg: IRegistry) {
    super.initialize(reg);
    this.functionModules = undefined;
    return this;
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: UnhandledClassicExceptionConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
    if (obj.getType() === "INTF" || file.getStructure() === undefined) {
      return [];
    }
 
    const syntax = new SyntaxLogic(this.reg, obj).run();
    if (syntax.issues.length > 0) {
      return [];
    }
 
    const methods = methodReferences(syntax.spaghetti.getTop(), file.getFilename());
 
    const issues: Issue[] = [];
    for (const statement of file.getStatements()) {
      if (statement.get() instanceof Statements.CallFunction) {
        issues.push(...this.checkCallFunction(statement, file));
        continue;
      }
 
      if (statement.get() instanceof Statements.Call) {
        const source = statement.findDirectExpression(Expressions.MethodSource);
        const body = statement.findDirectExpression(Expressions.MethodCallBody);
        const nameNode = source?.getLastChild();
        if (nameNode) {
          const token = nameNode.getFirstToken();
          const callee = nameNode.get() instanceof Expressions.Dynamic ? undefined : this.findMethod(methods, token);
          const issue = this.checkCall(file, token, callee, body?.findDirectExpression(Expressions.MethodParameters),
                                       body?.findDirectTokenByText("EXCEPTION") !== undefined, false, this.onlyLiterals(body));
          if (issue) {
            issues.push(issue);
          }
        }
      }
 
      for (const call of statement.findAllExpressionsRecursive(Expressions.MethodCall)) {
        const token = call.findDirectExpression(Expressions.MethodName)?.getFirstToken();
        if (token === undefined) {
          continue;
        }
        const param = call.findDirectExpression(Expressions.MethodCallParam);
        const parameters = param?.findDirectExpression(Expressions.MethodParameters);
        const issue = this.checkCall(file, token, this.findMethod(methods, token), parameters, false, false, this.onlyLiterals(param));
        if (issue) {
          issues.push(issue);
        }
      }
    }
 
    return issues;
  }
 
  private checkCallFunction(statement: StatementNode, file: ABAPFile): Issue[] {
    if (statement.findDirectTokenByText("STARTING")
        || statement.findDirectTokenByText("UPDATE")
        || statement.findDirectTokenByText("BACKGROUND")) {
      return [];
    }
 
    const nameNode = statement.findDirectExpression(Expressions.FunctionName);
    if (nameNode === undefined) {
      return [];
    }
 
    let callee: ICallee | undefined = undefined;
    let classBased = false;
    if (nameNode.findFirstExpression(Expressions.FieldChain) === undefined) {
      const name = nameNode.concatTokens().replace(/'/g, "").replace(/`/g, "");
      const module = this.findFunctionModule(name);
      if (module) {
        classBased = module.isClassBasedExceptions();
        callee = {
          name: module.getName(),
          exceptions: classBased ? [] : module.getExceptions().map(e => e.toUpperCase()),
        };
      }
    }
 
    const issues: Issue[] = [];
    const parameters = statement.findDirectExpression(Expressions.FunctionParameters);
    const exceptionTable = statement.findDirectTokenByText("EXCEPTION") !== undefined;
    const destination = statement.findDirectExpression(Expressions.Destination) !== undefined;
    const issue = this.checkCall(file, nameNode.getFirstToken(), callee, parameters, exceptionTable, destination, false);
    if (issue) {
      issues.push(issue);
    }
 
    if (this.conf.missingErrorMessage === true && exceptionTable === false && destination === false && classBased === false) {
      const listed = parameters?.findDirectExpression(Expressions.ParameterListExceptions)
        ?.findDirectExpressions(Expressions.ParameterException)
        .some(e => e.getFirstChild()!.concatTokens().toUpperCase() === "ERROR_MESSAGE");
      if (listed !== true) {
        const position = parameters?.findDirectTokenByText("EXCEPTIONS") || nameNode.getFirstToken();
        const message = "No error_message, OTHERS does not catch a message that the function module sends without RAISING";
        issues.push(Issue.atToken(file, position, message, this.getMetadata().key, this.conf.severity));
      }
    }
 
    return issues;
  }
 
  private checkCall(file: ABAPFile, token: AbstractToken, callee: ICallee | undefined,
                    parameters: ExpressionNode | undefined, exceptionTable: boolean, destination: boolean,
                    onlyLiterals: boolean): Issue | undefined {
    if (exceptionTable === true) {
      return undefined;
    }
 
    const list = parameters?.findDirectExpression(Expressions.ParameterListExceptions);
    if (list === undefined) {
      if (this.conf.missingExceptions === false
          || callee === undefined
          || callee.exceptions.length === 0
          || (this.conf.allowLiteralArguments === true && onlyLiterals === true)) {
        return undefined;
      }
      const message = `Classic exceptions of "${callee.name}" not handled: ${callee.exceptions.join(", ")}, call it with EXCEPTIONS`;
      return Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity);
    }
 
    if (this.conf.missingOthers === false) {
      return undefined;
    }
 
    const listed = list.findDirectExpressions(Expressions.ParameterException)
      .map(e => e.getFirstChild()!.concatTokens().toUpperCase());
    if (listed.includes("OTHERS")) {
      return undefined;
    }
 
    const position = parameters!.findDirectTokenByText("EXCEPTIONS") || list.getFirstToken();
 
    if (callee === undefined) {
      const message = "EXCEPTIONS without OTHERS, an exception that is not listed ends in a runtime error";
      return Issue.atToken(file, position, message, this.getMetadata().key, this.conf.severity);
    }
 
    const expected = [...callee.exceptions];
    if (destination === true) {
      expected.push("SYSTEM_FAILURE", "COMMUNICATION_FAILURE");
    }
    const missing = expected.filter(e => listed.includes(e) === false);
    if (missing.length === 0) {
      return undefined;
    }
    const message = `EXCEPTIONS without OTHERS, not mapped: ${missing.join(", ")}`;
    return Issue.atToken(file, position, message, this.getMetadata().key, this.conf.severity);
  }
 
  /** true if the call has arguments and all of them are literals or me, like describe_by_name( 'T100' ) */
  private onlyLiterals(node: ExpressionNode | undefined): boolean {
    if (node === undefined || node.findFirstExpression(Expressions.MethodCall) !== undefined) {
      return false;
    }
    const sources = node.findAllExpressions(Expressions.Source);
    return sources.length > 0
      && sources.every(s => (s.getChildren().length === 1 && s.getFirstChild()?.get() instanceof Expressions.Constant)
        || s.concatTokens().toUpperCase() === "ME");
  }
 
  private findMethod(methods: Map<string, MethodDefinition>, token: AbstractToken): ICallee | undefined {
    const method = methods.get(positionKey(token.getStart()));
    if (method === undefined) {
      return undefined;
    }
    return {
      name: method.getName(),
      exceptions: method.getExceptions().map(e => e.toUpperCase()),
    };
  }
 
  private findFunctionModule(name: string): FunctionModuleDefinition | undefined {
    if (this.functionModules === undefined) {
      this.functionModules = new Map<string, FunctionModuleDefinition>();
      for (const fugr of this.reg.getObjectsByType("FUGR")) {
        for (const module of (fugr as FunctionGroup).getModules()) {
          this.functionModules.set(module.getName().toUpperCase(), module);
        }
      }
    }
    return this.functionModules.get(name.toUpperCase());
  }
 
}