Is there any way to handle exceptions in karate? [duplicate] - exception

In a similar use case as in Read Karate config from YAML I want to read my environment config for Karate from a YAML file. This works well with karate.read. My extended use case now would be the following:
read environment config of common environments from a YAML file which is in version control
have a file with custom environments not in version control and read from that file too
for all environments (based on a ID field) that are defined in both files the custom environment definition overwrites the common one
I now have to read two files but for the file with the custom environments I don't know if it will exist because the user might choose to not have any custom environments defined. Is there a way to check if the second file exists before attempting to read it? I have checked the documentation for the karate object but have not found anything like that.
If that wouldn't be possible, is there another way how my use case could be implemented?

Karate has a built-in way to use dev-env specifc config that may not exist: https://github.com/intuit/karate#environment-specific-config
That said, note that you can catch exceptions in JS, so that gives you some more options: https://stackoverflow.com/a/54554175/143475
try {
// read
} catch(e) {
// print e if needed and ignore
}

Related

JSON Schema - How to define dependencies between properties located in different files?

I am working with JSON schema for a file, which keeps a set of variables we are using to define our configuration, which will be executed through Ansible. Importantly, we are using JSON schema to validate YAML files.
So far everything goes well. However, I have this challenge.
I have a file called common.yml and other called domain.yml. Inside domain.yml we have a property called domain_root, which depends on a property called common_dir, which is inside common.yml and I have not found any documentation on how to define a dependency when the property is in place, but in another file.
By the way, dependencies in the same file are working without issues.
"dependencies": {
"domain_home": ["domain_parent_dir", "domain_name"],
"domain_libraries":["domain_home"],
"logs_directory":["oracle_user", "domain_name"],
}
Please, if you have any clues, kindly help me.
Best regards,
RCC
You cannot. JSON Schema actually doesn't work with files at all. JSON Schema implementations may load a file to get to the JSON, but JSON Schema knows nothing about files in a filesystem.
In stead, consider combining your multiple files into a single file for validation purposes.
This doesn't help if you want validation in-editor, but could help if you only need validation as part of a continuious intergration (CI) process.

Gateway rest API resource can't find the file I provide

resource "aws_api_gateway_rest_api" "api" {
body = "${file("apigateway/json-resolved/swagger.json")}"
name = "api"
}
---------------------------------------------------------------------------------
Invalid value for "path" parameter: no file exists at apigateway/json-resolved/swagger.json;
this function works only with files that are distributed as
part of the configuration source code,
so if this file will be created by a resource in this configuration you must
instead obtain this result from an attribute of that resource.
When I try to deploy my API by providing the actual path to the API JSON, this is what it throws. Even though the file is there, even though I tried different paths, from relative to absolute, etc. It works when I paste the entire JSON in the body, but not when I provide a file. Why is that?
Since Terraform is not aware of the location of the file, you should specify it explicitly:
If the file is in the same directory, then use ./apigateway/json-resolved/swagger.json
If the file is one directory up from the directory you are running Terraform from, you could use ../apigateway/json-resolved/swagger.json
Alternatively, it is a good idea to use Terraform built-in functions for path manipulation: path.cwd, path.module, or path.root. More detailed explanation about what these three functions represent can be found in [1].
Provide a full path to the file by running pwd in the directory where the file is located (this works on Linux and MacOS) and paste the result of the command in the file function input.
Additionally, any combination of the points 2. and 3. could also work, but you should be careful.
There is also another great answer to a similar question [2].
NOTE: in some cases the path.* functions might not give expected results on Windows. As per this comment [3] from Github, if the paths are used consistently (i.e., all / or all \), Windows should also be able to work with path.* but only for versions of Terraform >=0.12. Based on the code snippet form the question it seems in this case an older version is used.
[1] https://www.terraform.io/language/expressions/references#filesystem-and-workspace-info
[2] Invalid value for "path" parameter: no file exists at
[3] https://github.com/hashicorp/terraform/issues/14986#issuecomment-448756885

Read properties file with JSON format

I have a Java code using selenium where I have a properties file which is in JSON format with multiple values and I want to use that file in Jenkins. For that I am using "This project is parameterized" option where I am selecting "File parameters" option.
So My question is How to use the JSON format in Jenkins? Am I doing is correct and what changes we have to make in code for that?
Can anyone help on this?
The "File parameters" is not working in the way you think, it is not like Jenkins will parse file and give you something like key/value map - no.
What is it doing is follwoing , you basically upload file and then how you use it is up to you, so in other words, if that file is for you java code, set the path for that file using the JVM params (e.g. -DpropertiesFilePath = ${abc.xyz}) and then Jenkins will parse the ${abc.xyz} for you and you java code will have proper path to file.
Otherwise, if you want to use the properties inside that JSON file itself for jenkins job configuration needs, then you have to write Jenkins job using either DSL or Jenkinsfile, in which having full access to file you can use for example JsonSlupper and parse Json file and assign properties to stages or whatever you need in Jenkins job walkthrow.

Read and Write file using vs code extension

i am building an extension to parse json using vs code extension.
so my need is ,it should be able able to load .json file from a particular folder and iterate through content of the file.
Then it should allow user to select few keys from it make a new json file out of this and save it in any folder.
But i am not able to find any way to read and write files in "vs code extension".Could someone please help me.
If you want to read the current edit state of a file you can use the following API workspace function:
vscode.workspace.openTextDocument(uri).then((document) => {
let text = document.getText();
});
This will show you the current state of the file including unpersisted changes. document is of type TextDocument and has isDirty set to true if it has pending changes.
Since the extension runs in nodejs, you should be able to use any nodejs module built-in or installed by npm in the usual way.
For your purpose you will be OK with the built-in fs module: https://nodejs.org/dist/latest-v6.x/docs/api/fs.html
In your extension you will need to import the required module, so your code file should contain this:
let fs = require("fs");
and then use the methods in the usual way, eg. fs.fileReadSync( filename, encoding ) ...
Please not that there is one exception. If you install a nodejs module containing compiled, binary code, it will not run in the extension and instead you will see an error message saying something like %1 is not a valid Win32 application. Pure javascript modules are OK, though.
VSCode extensions are running in node.js. Therefore you can use any available node.js package/module within your extension. For instance, check out this question for reading JSON.
For JSON, you just need to require or import the JSON file, such as:
const jsonObject = require('./myJSONfile.json');
// do something
For JSON with comments, you can use node-jsonc-parser.
After the manipulation, you could use the fs module of nodej.js to write to the disk.

Symfony 2 Config File Types

I generated a bundle and set it to use annotations for config files. But I find that in Resources/config/services.xml, its still XML? Perhaps it because I cant configure services using annotations? If so how can I make it use yml if thats the case? I could just add a YML file but I dont suppose it will be detected?
Your assumption that you can't define a service using Annotations is correct. You need to have either a services.xml or services.yml under Resources/config for the bundle.
Check out the answers to this question, it covers how to define a service using both xml, yml and through a config.yml import.
I've also written an answer here on the basics of defining a service.